|
|
@ -24,11 +24,8 @@ import multiprocessing
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import traceback
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
import paramiko # non-core dependency
|
|
|
|
# non-core
|
|
|
|
import ansible.constants as C
|
|
|
|
import paramiko
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import constants as C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _executor_hook(x):
|
|
|
|
def _executor_hook(x):
|
|
|
|
''' callback used by multiprocessing pool '''
|
|
|
|
''' callback used by multiprocessing pool '''
|
|
|
@ -48,10 +45,17 @@ class Runner(object):
|
|
|
|
remote_user=C.DEFAULT_REMOTE_USER,
|
|
|
|
remote_user=C.DEFAULT_REMOTE_USER,
|
|
|
|
remote_pass=C.DEFAULT_REMOTE_PASS,
|
|
|
|
remote_pass=C.DEFAULT_REMOTE_PASS,
|
|
|
|
verbose=False):
|
|
|
|
verbose=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
Constructor
|
|
|
|
Constructor.
|
|
|
|
host_list -- file on disk listing hosts to manage, or an array of hostnames
|
|
|
|
|
|
|
|
pattern ------ a fnmatch pattern selecting some of the hosts in host_list
|
|
|
|
|
|
|
|
module_path -- location of ansible library on disk
|
|
|
|
|
|
|
|
module_name -- which module to run
|
|
|
|
|
|
|
|
module_args -- arguments to pass to module
|
|
|
|
|
|
|
|
forks -------- how parallel should we be? 1 is extra debuggable.
|
|
|
|
|
|
|
|
remote_user -- who to login as (default root)
|
|
|
|
|
|
|
|
remote_pass -- provide only if you don't want to use keys or ssh-agent
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
self.host_list = self._parse_hosts(host_list)
|
|
|
|
self.host_list = self._parse_hosts(host_list)
|
|
|
@ -109,44 +113,52 @@ class Runner(object):
|
|
|
|
for filename in files:
|
|
|
|
for filename in files:
|
|
|
|
self._exec_command(conn, "rm -f %s" % filename)
|
|
|
|
self._exec_command(conn, "rm -f %s" % filename)
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_normal_module(self, conn, host):
|
|
|
|
def _transfer_file(self, conn, source, dest):
|
|
|
|
''' transfer a module, set it executable, and run it '''
|
|
|
|
self.remote_log(conn, 'COPY remote:%s local:%s' % (source, dest))
|
|
|
|
|
|
|
|
ftp = conn.open_sftp()
|
|
|
|
|
|
|
|
ftp.put(source, dest)
|
|
|
|
|
|
|
|
ftp.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _transfer_module(self, conn):
|
|
|
|
outpath = self._copy_module(conn)
|
|
|
|
outpath = self._copy_module(conn)
|
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath)
|
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath)
|
|
|
|
|
|
|
|
return outpath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_module(self, conn, outpath):
|
|
|
|
cmd = self._command(outpath)
|
|
|
|
cmd = self._command(outpath)
|
|
|
|
result = self._exec_command(conn, cmd)
|
|
|
|
result = self._exec_command(conn, cmd)
|
|
|
|
self._delete_remote_files(conn, outpath)
|
|
|
|
self._delete_remote_files(conn, [ outpath ])
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_normal_module(self, conn, host):
|
|
|
|
|
|
|
|
''' transfer a module, set it executable, and run it '''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module = self._transfer_module(conn)
|
|
|
|
|
|
|
|
result = self._execute_module(conn, module)
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_copy(self, conn, host):
|
|
|
|
def _execute_copy(self, conn, host):
|
|
|
|
''' transfer a file + copy module, run copy module, clean up '''
|
|
|
|
''' handler for file transfer operations '''
|
|
|
|
|
|
|
|
|
|
|
|
self.remote_log(conn, 'COPY remote:%s local:%s' % (self.module_args[0], self.module_args[1]))
|
|
|
|
# transfer the file to a remote tmp location
|
|
|
|
source = self.module_args[0]
|
|
|
|
source = self.module_args[0]
|
|
|
|
dest = self.module_args[1]
|
|
|
|
dest = self.module_args[1]
|
|
|
|
tmp_dest = self._get_tmp_path(conn, dest.split("/")[-1])
|
|
|
|
tmp_dest = self._get_tmp_path(conn, dest.split("/")[-1])
|
|
|
|
|
|
|
|
self._transfer_file(conn, source, tmp_dest)
|
|
|
|
ftp = conn.open_sftp()
|
|
|
|
|
|
|
|
ftp.put(source, tmp_dest)
|
|
|
|
|
|
|
|
ftp.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# install the copy module
|
|
|
|
# install the copy module
|
|
|
|
|
|
|
|
|
|
|
|
self.module_name = 'copy'
|
|
|
|
self.module_name = 'copy'
|
|
|
|
outpath = self._copy_module(conn)
|
|
|
|
module = self._transfer_module(conn)
|
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# run the copy module
|
|
|
|
# run the copy module
|
|
|
|
|
|
|
|
|
|
|
|
self.module_args = [ tmp_dest, dest ]
|
|
|
|
self.module_args = [ tmp_dest, dest ]
|
|
|
|
cmd = self._command(outpath)
|
|
|
|
result = self._execute_module(conn, module)
|
|
|
|
result = self._exec_command(conn, cmd)
|
|
|
|
self._delete_remote_files(conn, tmp_dest)
|
|
|
|
self._delete_remote_files(conn, [outpath, tmp_dest])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
|
|
|
|
|
|
|
|
def _execute_template(self, conn, host):
|
|
|
|
def _execute_template(self, conn, host):
|
|
|
|
|
|
|
|
''' handler for template operations '''
|
|
|
|
|
|
|
|
|
|
|
|
source = self.module_args[0]
|
|
|
|
source = self.module_args[0]
|
|
|
|
dest = self.module_args[1]
|
|
|
|
dest = self.module_args[1]
|
|
|
|
metadata = '/etc/ansible/setup'
|
|
|
|
metadata = '/etc/ansible/setup'
|
|
|
@ -154,21 +166,16 @@ class Runner(object):
|
|
|
|
# first copy the source template over
|
|
|
|
# first copy the source template over
|
|
|
|
tempname = os.path.split(source)[-1]
|
|
|
|
tempname = os.path.split(source)[-1]
|
|
|
|
temppath = self._get_tmp_path(conn, tempname)
|
|
|
|
temppath = self._get_tmp_path(conn, tempname)
|
|
|
|
self.remote_log(conn, 'COPY remote:%s local:%s' % (source, temppath))
|
|
|
|
self._transfer_file(conn, source, temppath)
|
|
|
|
ftp = conn.open_sftp()
|
|
|
|
|
|
|
|
ftp.put(source, temppath)
|
|
|
|
|
|
|
|
ftp.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# install the template module
|
|
|
|
# install the template module
|
|
|
|
self.module_name = 'template'
|
|
|
|
self.module_name = 'template'
|
|
|
|
outpath = self._copy_module(conn)
|
|
|
|
module = self._transfer_module(conn)
|
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# run the template module
|
|
|
|
# run the template module
|
|
|
|
self.module_args = [ temppath, dest, metadata ]
|
|
|
|
self.module_args = [ temppath, dest, metadata ]
|
|
|
|
result = self._exec_command(conn, self._command(outpath))
|
|
|
|
result = self._execute_module(conn, module)
|
|
|
|
# clean up
|
|
|
|
self._delete_remote_files(conn, [ temppath ])
|
|
|
|
self._delete_remote_files(conn, [ outpath, temppath ])
|
|
|
|
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
return self._return_from_module(conn, host, result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|