2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 04:28:58 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2012-02-24 04:28:58 +00:00
|
|
|
#
|
|
|
|
|
2012-03-03 03:03:03 +00:00
|
|
|
################################################
|
|
|
|
|
2012-02-24 04:28:58 +00:00
|
|
|
import fnmatch
|
|
|
|
import multiprocessing
|
2012-02-27 05:43:02 +00:00
|
|
|
import signal
|
2012-02-24 04:28:58 +00:00
|
|
|
import os
|
2012-02-28 05:45:37 +00:00
|
|
|
import Queue
|
2012-03-03 17:25:56 +00:00
|
|
|
import random
|
2012-03-14 00:59:05 +00:00
|
|
|
import traceback
|
2012-03-14 04:34:00 +00:00
|
|
|
import tempfile
|
2012-03-30 23:06:14 +00:00
|
|
|
import subprocess
|
2012-03-13 03:11:54 +00:00
|
|
|
|
2012-03-18 21:53:58 +00:00
|
|
|
import ansible.constants as C
|
|
|
|
import ansible.connection
|
2012-03-18 21:04:07 +00:00
|
|
|
from ansible import utils
|
2012-03-18 21:16:12 +00:00
|
|
|
from ansible import errors
|
2012-03-25 23:05:27 +00:00
|
|
|
from ansible import callbacks as ans_callbacks
|
2012-03-29 00:32:04 +00:00
|
|
|
|
|
|
|
HAS_ATFORK=True
|
|
|
|
try:
|
|
|
|
from Crypto.Random import atfork
|
|
|
|
except ImportError:
|
|
|
|
HAS_ATFORK=False
|
2012-03-14 23:57:56 +00:00
|
|
|
|
2012-03-03 03:03:03 +00:00
|
|
|
################################################
|
2012-02-24 04:28:58 +00:00
|
|
|
|
2012-02-27 05:43:02 +00:00
|
|
|
def _executor_hook(job_queue, result_queue):
|
2012-02-24 04:28:58 +00:00
|
|
|
''' callback used by multiprocessing pool '''
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2012-03-29 00:32:04 +00:00
|
|
|
# attempt workaround of https://github.com/newsapps/beeswithmachineguns/issues/17
|
|
|
|
# does not occur for everyone, some claim still occurs on newer paramiko
|
|
|
|
# this function not present in CentOS 6
|
|
|
|
if HAS_ATFORK:
|
|
|
|
atfork()
|
|
|
|
|
2012-02-27 05:43:02 +00:00
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
while not job_queue.empty():
|
|
|
|
try:
|
|
|
|
job = job_queue.get(block=False)
|
|
|
|
runner, host = job
|
|
|
|
result_queue.put(runner._executor(host))
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
2012-03-25 23:05:27 +00:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
################################################
|
2012-02-24 04:28:58 +00:00
|
|
|
|
|
|
|
class Runner(object):
|
|
|
|
|
2012-03-31 15:02:10 +00:00
|
|
|
_external_variable_script = None
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def __init__(self, host_list=C.DEFAULT_HOST_LIST, module_path=C.DEFAULT_MODULE_PATH,
|
|
|
|
module_name=C.DEFAULT_MODULE_NAME, module_args=C.DEFAULT_MODULE_ARGS,
|
|
|
|
forks=C.DEFAULT_FORKS, timeout=C.DEFAULT_TIMEOUT, pattern=C.DEFAULT_PATTERN,
|
|
|
|
remote_user=C.DEFAULT_REMOTE_USER, remote_pass=C.DEFAULT_REMOTE_PASS,
|
2012-03-28 21:05:31 +00:00
|
|
|
remote_port=C.DEFAULT_REMOTE_PORT, background=0, basedir=None, setup_cache=None,
|
2012-03-29 02:51:16 +00:00
|
|
|
transport='paramiko', conditional='True', groups={}, callbacks=None, verbose=False,
|
2012-04-04 11:38:21 +00:00
|
|
|
debug=False, sudo=False, extra_vars=None, module_vars=None):
|
2012-04-04 00:20:55 +00:00
|
|
|
|
2012-03-20 02:42:31 +00:00
|
|
|
if setup_cache is None:
|
|
|
|
setup_cache = {}
|
2012-03-22 03:39:09 +00:00
|
|
|
if basedir is None:
|
|
|
|
basedir = os.getcwd()
|
2012-03-22 04:30:05 +00:00
|
|
|
|
2012-03-25 23:05:27 +00:00
|
|
|
if callbacks is None:
|
|
|
|
callbacks = ans_callbacks.DefaultRunnerCallbacks()
|
|
|
|
self.callbacks = callbacks
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
self.generated_jid = str(random.randint(0, 999999999999))
|
|
|
|
self.connector = ansible.connection.Connection(self, transport)
|
2012-03-22 04:30:05 +00:00
|
|
|
|
|
|
|
if type(host_list) == str:
|
|
|
|
self.host_list, self.groups = self.parse_hosts(host_list)
|
|
|
|
else:
|
|
|
|
self.host_list = host_list
|
|
|
|
self.groups = groups
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
self.setup_cache = setup_cache
|
|
|
|
self.conditional = conditional
|
2012-02-25 22:16:23 +00:00
|
|
|
self.module_path = module_path
|
|
|
|
self.module_name = module_name
|
2012-04-04 14:27:24 +00:00
|
|
|
self.forks = int(forks)
|
2012-02-25 22:16:23 +00:00
|
|
|
self.pattern = pattern
|
|
|
|
self.module_args = module_args
|
2012-04-04 11:38:21 +00:00
|
|
|
self.module_vars = module_vars
|
2012-03-31 02:28:10 +00:00
|
|
|
self.extra_vars = extra_vars
|
2012-02-25 22:16:23 +00:00
|
|
|
self.timeout = timeout
|
2012-03-30 20:17:16 +00:00
|
|
|
self.debug = debug
|
2012-02-25 22:16:23 +00:00
|
|
|
self.verbose = verbose
|
|
|
|
self.remote_user = remote_user
|
|
|
|
self.remote_pass = remote_pass
|
2012-03-28 21:05:31 +00:00
|
|
|
self.remote_port = remote_port
|
2012-03-22 03:39:09 +00:00
|
|
|
self.background = background
|
2012-03-29 02:51:16 +00:00
|
|
|
self.basedir = basedir
|
|
|
|
self.sudo = sudo
|
2012-03-06 01:09:03 +00:00
|
|
|
|
2012-03-31 02:47:58 +00:00
|
|
|
if type(self.module_args) != str:
|
|
|
|
raise Exception("module_args must be a string: %s" % self.module_args)
|
|
|
|
|
2012-03-02 01:41:17 +00:00
|
|
|
self._tmp_paths = {}
|
2012-03-03 17:25:56 +00:00
|
|
|
random.seed()
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
@classmethod
|
2012-03-22 04:30:05 +00:00
|
|
|
def parse_hosts_from_regular_file(cls, host_list):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' parse a textual host file '''
|
|
|
|
|
2012-03-22 04:30:05 +00:00
|
|
|
results = []
|
|
|
|
groups = dict(ungrouped=[])
|
2012-03-22 03:39:09 +00:00
|
|
|
lines = file(host_list).read().split("\n")
|
|
|
|
group_name = 'ungrouped'
|
|
|
|
for item in lines:
|
|
|
|
item = item.lstrip().rstrip()
|
|
|
|
if item.startswith("#"):
|
|
|
|
# ignore commented out lines
|
2012-03-22 04:30:05 +00:00
|
|
|
pass
|
|
|
|
elif item.startswith("["):
|
2012-03-22 03:39:09 +00:00
|
|
|
# looks like a group
|
|
|
|
group_name = item.replace("[","").replace("]","").lstrip().rstrip()
|
|
|
|
groups[group_name] = []
|
|
|
|
elif item != "":
|
|
|
|
# looks like a regular host
|
|
|
|
groups[group_name].append(item)
|
2012-03-22 04:30:05 +00:00
|
|
|
if not item in results:
|
|
|
|
results.append(item)
|
|
|
|
return (results, groups)
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
@classmethod
|
2012-03-31 02:28:10 +00:00
|
|
|
def parse_hosts_from_script(cls, host_list, extra_vars):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' evaluate a script that returns list of hosts by groups '''
|
|
|
|
|
2012-03-22 04:30:05 +00:00
|
|
|
results = []
|
|
|
|
groups = dict(ungrouped=[])
|
2012-03-22 03:39:09 +00:00
|
|
|
host_list = os.path.abspath(host_list)
|
|
|
|
cls._external_variable_script = host_list
|
2012-03-31 02:28:10 +00:00
|
|
|
cmd = [host_list, '--list']
|
|
|
|
if extra_vars:
|
|
|
|
cmd.extend(['--extra-vars', extra_vars])
|
|
|
|
cmd = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
|
2012-03-22 03:39:09 +00:00
|
|
|
out, err = cmd.communicate()
|
2012-04-02 21:56:09 +00:00
|
|
|
rc = cmd.returncode
|
|
|
|
if rc:
|
|
|
|
raise errors.AnsibleError("%s: %s" % (host_list, err))
|
2012-03-22 03:39:09 +00:00
|
|
|
try:
|
|
|
|
groups = utils.json_loads(out)
|
|
|
|
except:
|
|
|
|
raise errors.AnsibleError("invalid JSON response from script: %s" % host_list)
|
|
|
|
for (groupname, hostlist) in groups.iteritems():
|
|
|
|
for host in hostlist:
|
|
|
|
if host not in results:
|
|
|
|
results.append(host)
|
2012-03-22 04:30:05 +00:00
|
|
|
return (results, groups)
|
2012-03-03 17:25:56 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-02-28 03:28:01 +00:00
|
|
|
@classmethod
|
2012-03-31 02:28:10 +00:00
|
|
|
def parse_hosts(cls, host_list, override_hosts=None, extra_vars=None):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' parse the host inventory file, returns (hosts, groups) '''
|
2012-03-02 01:41:17 +00:00
|
|
|
|
2012-03-24 20:19:38 +00:00
|
|
|
if override_hosts is not None:
|
|
|
|
if type(override_hosts) != list:
|
|
|
|
raise errors.AnsibleError("override hosts must be a list")
|
|
|
|
return (override_hosts, dict(ungrouped=override_hosts))
|
|
|
|
|
2012-03-02 01:41:17 +00:00
|
|
|
if type(host_list) == list:
|
2012-03-22 04:30:05 +00:00
|
|
|
raise Exception("function can only be called on inventory files")
|
2012-02-27 01:29:27 +00:00
|
|
|
|
2012-03-02 01:41:17 +00:00
|
|
|
host_list = os.path.expanduser(host_list)
|
2012-03-13 03:11:54 +00:00
|
|
|
if not os.path.exists(host_list):
|
2012-03-18 21:16:12 +00:00
|
|
|
raise errors.AnsibleFileNotFound("inventory file not found: %s" % host_list)
|
2012-03-13 03:11:54 +00:00
|
|
|
|
2012-03-30 23:06:14 +00:00
|
|
|
if not os.access(host_list, os.X_OK):
|
2012-03-22 04:30:05 +00:00
|
|
|
return Runner.parse_hosts_from_regular_file(host_list)
|
2012-03-30 23:06:14 +00:00
|
|
|
else:
|
2012-03-31 02:28:10 +00:00
|
|
|
return Runner.parse_hosts_from_script(host_list, extra_vars)
|
2012-02-24 04:28:58 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
2012-02-24 04:28:58 +00:00
|
|
|
|
2012-03-22 04:30:05 +00:00
|
|
|
def _matches(self, host_name, pattern):
|
2012-02-25 22:16:23 +00:00
|
|
|
''' returns if a hostname is matched by the pattern '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-02-27 01:29:27 +00:00
|
|
|
# a pattern is in fnmatch format but more than one pattern
|
|
|
|
# can be strung together with semicolons. ex:
|
|
|
|
# atlanta-web*.example.com;dc-web*.example.com
|
2012-03-02 01:41:17 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
if host_name == '':
|
|
|
|
return False
|
2012-03-03 19:26:59 +00:00
|
|
|
pattern = pattern.replace(";",":")
|
|
|
|
subpatterns = pattern.split(":")
|
2012-02-25 22:22:48 +00:00
|
|
|
for subpattern in subpatterns:
|
2012-03-22 04:30:05 +00:00
|
|
|
if subpattern == 'all':
|
|
|
|
return True
|
|
|
|
if fnmatch.fnmatch(host_name, subpattern):
|
2012-03-03 00:44:50 +00:00
|
|
|
return True
|
2012-03-22 03:39:09 +00:00
|
|
|
elif subpattern in self.groups:
|
2012-03-02 01:41:17 +00:00
|
|
|
if host_name in self.groups[subpattern]:
|
|
|
|
return True
|
2012-02-25 22:16:23 +00:00
|
|
|
return False
|
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
def _connect(self, host):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' connects to a host, returns (is_successful, connection_object OR traceback_string) '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
try:
|
2012-03-10 18:35:46 +00:00
|
|
|
return [ True, self.connector.connect(host) ]
|
2012-03-18 21:16:12 +00:00
|
|
|
except errors.AnsibleConnectionFailed, e:
|
2012-04-04 14:57:54 +00:00
|
|
|
return [ False, "FAILED: %s" % str(e) ]
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
def _return_from_module(self, conn, host, result, err, executed=None):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' helper function to handle JSON parsing of results '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
try:
|
2012-03-21 02:29:21 +00:00
|
|
|
result = utils.parse_json(result)
|
|
|
|
if executed is not None:
|
|
|
|
result['invocation'] = executed
|
2012-03-30 20:17:16 +00:00
|
|
|
if 'stderr' in result:
|
|
|
|
err="%s%s"%(err,result['stderr'])
|
|
|
|
return [host, True, result, err]
|
2012-04-04 14:57:54 +00:00
|
|
|
except Exception, e:
|
2012-03-30 20:17:16 +00:00
|
|
|
return [host, False, "%s/%s/%s" % (str(e), result, executed), err]
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
def _delete_remote_files(self, conn, files):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' deletes one or more remote files '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
if type(files) == str:
|
|
|
|
files = [ files ]
|
2012-02-25 22:16:23 +00:00
|
|
|
for filename in files:
|
2012-03-03 17:25:56 +00:00
|
|
|
if not filename.startswith('/tmp/'):
|
|
|
|
raise Exception("not going to happen")
|
2012-03-30 02:58:10 +00:00
|
|
|
self._exec_command(conn, "rm -rf %s" % filename, None)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
def _transfer_module(self, conn, tmp, module):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' transfers a module file to the remote side to execute it, but does not execute it yet '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
outpath = self._copy_module(conn, tmp, module)
|
2012-03-30 02:58:10 +00:00
|
|
|
self._exec_command(conn, "chmod +x %s" % outpath, tmp)
|
2012-02-25 22:16:23 +00:00
|
|
|
return outpath
|
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-04-04 11:36:21 +00:00
|
|
|
def _transfer_str(self, conn, tmp, name, args_str):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' transfer arguments as a single file to be fed to the module. '''
|
|
|
|
|
2012-03-14 23:05:19 +00:00
|
|
|
args_fd, args_file = tempfile.mkstemp()
|
|
|
|
args_fo = os.fdopen(args_fd, 'w')
|
|
|
|
args_fo.write(args_str)
|
|
|
|
args_fo.flush()
|
|
|
|
args_fo.close()
|
2012-03-22 03:39:09 +00:00
|
|
|
|
2012-04-04 11:36:21 +00:00
|
|
|
args_remote = os.path.join(tmp, name)
|
2012-03-29 02:51:16 +00:00
|
|
|
conn.put_file(args_file, args_remote)
|
2012-03-22 03:39:09 +00:00
|
|
|
os.unlink(args_file)
|
|
|
|
|
2012-03-14 23:05:19 +00:00
|
|
|
return args_remote
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def _add_variables_from_script(self, conn, inject):
|
|
|
|
''' support per system variabes from external variable scripts, see web docs '''
|
2012-03-20 23:55:04 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
host = conn.host
|
2012-03-31 02:28:10 +00:00
|
|
|
|
|
|
|
cmd = [Runner._external_variable_script, '--host', host]
|
|
|
|
if self.extra_vars:
|
|
|
|
cmd.extend(['--extra-vars', self.extra_vars])
|
|
|
|
|
|
|
|
cmd = subprocess.Popen(cmd,
|
2012-03-22 03:39:09 +00:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
shell=False
|
2012-03-22 00:00:48 +00:00
|
|
|
)
|
2012-03-22 03:39:09 +00:00
|
|
|
out, err = cmd.communicate()
|
|
|
|
inject2 = {}
|
|
|
|
try:
|
|
|
|
inject2 = utils.json_loads(out)
|
|
|
|
except:
|
|
|
|
raise errors.AnsibleError("%s returned invalid result when called with hostname %s" % (
|
|
|
|
Runner._external_variable_script,
|
|
|
|
host
|
|
|
|
))
|
|
|
|
# store injected variables in the templates
|
|
|
|
inject.update(inject2)
|
2012-03-06 03:23:56 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
# *****************************************************
|
2012-03-21 02:29:21 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def _add_setup_vars(self, inject, args):
|
|
|
|
''' setup module variables need special handling '''
|
|
|
|
|
|
|
|
for (k,v) in inject.iteritems():
|
|
|
|
if not k.startswith('facter_') and not k.startswith('ohai_'):
|
|
|
|
if str(v).find(" ") != -1:
|
|
|
|
v = "\"%s\"" % v
|
2012-03-22 00:00:48 +00:00
|
|
|
args += " %s=%s" % (k, str(v).replace(" ","~~~"))
|
2012-03-22 03:47:58 +00:00
|
|
|
return args
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
# *****************************************************
|
2012-03-30 23:06:14 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def _add_setup_metadata(self, args):
|
|
|
|
''' automatically determine where to store variables for the setup module '''
|
|
|
|
|
|
|
|
if args.find("metadata=") == -1:
|
2012-03-06 03:23:56 +00:00
|
|
|
if self.remote_user == 'root':
|
|
|
|
args = "%s metadata=/etc/ansible/setup" % args
|
|
|
|
else:
|
2012-03-30 02:58:10 +00:00
|
|
|
args = "%s metadata=/home/%s/.ansible/setup" % (args, self.remote_user)
|
2012-03-22 03:47:58 +00:00
|
|
|
return args
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _execute_module(self, conn, tmp, remote_module_path, args,
|
|
|
|
async_jid=None, async_module=None, async_limit=None):
|
|
|
|
''' runs a module that has already been transferred '''
|
|
|
|
|
|
|
|
inject = self.setup_cache.get(conn.host,{})
|
|
|
|
conditional = utils.double_template(self.conditional, inject)
|
|
|
|
if not eval(conditional):
|
2012-03-30 20:17:16 +00:00
|
|
|
return [ utils.smjson(dict(skipped=True)), None, 'skipped' ]
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
if Runner._external_variable_script is not None:
|
|
|
|
self._add_variables_from_script(conn, inject)
|
|
|
|
if self.module_name == 'setup':
|
2012-03-22 03:47:58 +00:00
|
|
|
args = self._add_setup_vars(inject, args)
|
|
|
|
args = self._add_setup_metadata(args)
|
2012-03-06 03:23:56 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
args = utils.template(args, inject)
|
2012-03-21 02:29:21 +00:00
|
|
|
module_name_tail = remote_module_path.split("/")[-1]
|
|
|
|
client_executed_str = "%s %s" % (module_name_tail, args.strip())
|
2012-03-06 01:09:03 +00:00
|
|
|
|
2012-04-04 11:36:21 +00:00
|
|
|
argsfile = self._transfer_str(conn, tmp, 'arguments', args)
|
2012-03-14 23:57:56 +00:00
|
|
|
if async_jid is None:
|
|
|
|
cmd = "%s %s" % (remote_module_path, argsfile)
|
|
|
|
else:
|
2012-03-22 03:39:09 +00:00
|
|
|
cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]])
|
2012-04-02 17:29:12 +00:00
|
|
|
|
|
|
|
# log command as the full command not as the path to args file - helps with debugging
|
|
|
|
msg = '%s: "%s"' % (self.module_name, args)
|
|
|
|
conn.exec_command('/usr/bin/logger -t ansible -p auth.info "%s"' % msg, None)
|
|
|
|
|
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
res, err = self._exec_command(conn, cmd, tmp, sudoable=True)
|
|
|
|
return ( res, err, client_executed_str )
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _add_result_to_setup_cache(self, conn, result):
|
|
|
|
''' allows discovered variables to be used in templates and action statements '''
|
|
|
|
|
|
|
|
host = conn.host
|
|
|
|
try:
|
|
|
|
var_result = utils.parse_json(result)
|
|
|
|
except:
|
|
|
|
var_result = {}
|
|
|
|
|
|
|
|
# note: do not allow variables from playbook to be stomped on
|
|
|
|
# by variables coming up from facter/ohai/etc. They
|
|
|
|
# should be prefixed anyway
|
|
|
|
if not host in self.setup_cache:
|
|
|
|
self.setup_cache[host] = {}
|
|
|
|
for (k, v) in var_result.iteritems():
|
|
|
|
if not k in self.setup_cache[host]:
|
|
|
|
self.setup_cache[host][k] = v
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-20 02:42:31 +00:00
|
|
|
def _execute_normal_module(self, conn, host, tmp, module_name):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' transfer & execute a module that is not 'copy' or 'template' '''
|
2012-03-06 03:23:56 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
# shell and command are the same module
|
2012-03-20 02:42:31 +00:00
|
|
|
if module_name == 'shell':
|
|
|
|
module_name = 'command'
|
2012-03-31 02:47:58 +00:00
|
|
|
self.module_args += " #USE_SHELL"
|
2012-03-15 00:40:06 +00:00
|
|
|
|
2012-03-20 02:42:31 +00:00
|
|
|
module = self._transfer_module(conn, tmp, module_name)
|
2012-03-30 20:17:16 +00:00
|
|
|
(result, err, executed) = self._execute_module(conn, tmp, module, self.module_args)
|
2012-03-06 03:23:56 +00:00
|
|
|
|
2012-03-20 02:42:31 +00:00
|
|
|
if module_name == 'setup':
|
2012-03-22 03:39:09 +00:00
|
|
|
self._add_result_to_setup_cache(conn, result)
|
2012-03-18 21:24:09 +00:00
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
return self._return_from_module(conn, host, result, err, executed)
|
2012-03-03 17:25:56 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-20 02:42:31 +00:00
|
|
|
def _execute_async_module(self, conn, host, tmp, module_name):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' transfer the given module name, plus the async module, then run it '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-18 23:25:56 +00:00
|
|
|
# hack to make the 'shell' module keyword really be executed
|
|
|
|
# by the command module
|
2012-03-20 02:42:31 +00:00
|
|
|
module_args = self.module_args
|
|
|
|
if module_name == 'shell':
|
|
|
|
module_name = 'command'
|
2012-03-31 02:47:58 +00:00
|
|
|
module_args += " #USE_SHELL"
|
2012-03-18 23:25:56 +00:00
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
async = self._transfer_module(conn, tmp, 'async_wrapper')
|
2012-03-20 02:42:31 +00:00
|
|
|
module = self._transfer_module(conn, tmp, module_name)
|
2012-03-30 20:17:16 +00:00
|
|
|
(result, err, executed) = self._execute_module(conn, tmp, async, module_args,
|
2012-03-14 23:57:56 +00:00
|
|
|
async_module=module,
|
|
|
|
async_jid=self.generated_jid,
|
|
|
|
async_limit=self.background
|
|
|
|
)
|
2012-03-21 02:29:21 +00:00
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
return self._return_from_module(conn, host, result, err, executed)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-03 03:38:55 +00:00
|
|
|
def _execute_copy(self, conn, host, tmp):
|
2012-02-25 22:16:23 +00:00
|
|
|
''' handler for file transfer operations '''
|
|
|
|
|
2012-02-27 01:29:27 +00:00
|
|
|
# load up options
|
2012-03-22 03:39:09 +00:00
|
|
|
options = utils.parse_kv(self.module_args)
|
2012-02-27 00:21:44 +00:00
|
|
|
source = options['src']
|
|
|
|
dest = options['dest']
|
2012-02-27 01:29:27 +00:00
|
|
|
|
|
|
|
# transfer the file to a remote tmp location
|
2012-03-22 03:39:09 +00:00
|
|
|
tmp_src = tmp + source.split('/')[-1]
|
2012-03-29 02:51:16 +00:00
|
|
|
conn.put_file(utils.path_dwim(self.basedir, source), tmp_src)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
|
|
|
# install the copy module
|
|
|
|
self.module_name = 'copy'
|
2012-03-03 17:25:56 +00:00
|
|
|
module = self._transfer_module(conn, tmp, 'copy')
|
2012-02-25 22:16:23 +00:00
|
|
|
|
|
|
|
# run the copy module
|
2012-03-31 02:47:58 +00:00
|
|
|
args = "src=%s dest=%s" % (tmp_src, dest)
|
2012-03-30 20:17:16 +00:00
|
|
|
(result1, err, executed) = self._execute_module(conn, tmp, module, args)
|
|
|
|
(host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed)
|
2012-03-16 02:32:14 +00:00
|
|
|
|
|
|
|
if ok:
|
2012-03-30 20:17:16 +00:00
|
|
|
return self._chain_file_module(conn, tmp, data, err, options, executed)
|
2012-03-16 02:32:14 +00:00
|
|
|
else:
|
2012-03-30 20:17:16 +00:00
|
|
|
return (host, ok, data, err)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
def _chain_file_module(self, conn, tmp, data, err, options, executed):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' handles changing file attribs after copy/template operations '''
|
|
|
|
|
|
|
|
old_changed = data.get('changed', False)
|
|
|
|
module = self._transfer_module(conn, tmp, 'file')
|
2012-03-31 02:47:58 +00:00
|
|
|
args = ' '.join([ "%s=%s" % (k,v) for (k,v) in options.items() ])
|
2012-03-30 20:17:16 +00:00
|
|
|
(result2, err2, executed2) = self._execute_module(conn, tmp, module, args)
|
|
|
|
results2 = self._return_from_module(conn, conn.host, result2, err2, executed)
|
|
|
|
(host, ok, data2, err2) = results2
|
2012-03-22 03:39:09 +00:00
|
|
|
new_changed = data2.get('changed', False)
|
|
|
|
data.update(data2)
|
|
|
|
if old_changed or new_changed:
|
|
|
|
data['changed'] = True
|
2012-03-30 20:17:16 +00:00
|
|
|
return (host, ok, data, "%s%s"%(err,err2))
|
2012-03-22 03:39:09 +00:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-03 03:38:55 +00:00
|
|
|
def _execute_template(self, conn, host, tmp):
|
2012-02-25 22:16:23 +00:00
|
|
|
''' handler for template operations '''
|
|
|
|
|
2012-02-27 01:29:27 +00:00
|
|
|
# load up options
|
2012-03-22 03:39:09 +00:00
|
|
|
options = utils.parse_kv(self.module_args)
|
2012-02-27 00:21:44 +00:00
|
|
|
source = options['src']
|
|
|
|
dest = options['dest']
|
2012-03-06 03:23:56 +00:00
|
|
|
metadata = options.get('metadata', None)
|
|
|
|
|
|
|
|
if metadata is None:
|
|
|
|
if self.remote_user == 'root':
|
|
|
|
metadata = '/etc/ansible/setup'
|
|
|
|
else:
|
|
|
|
metadata = '~/.ansible/setup'
|
2012-02-25 22:16:23 +00:00
|
|
|
|
|
|
|
# first copy the source template over
|
2012-03-22 03:39:09 +00:00
|
|
|
temppath = tmp + os.path.split(source)[-1]
|
2012-03-29 02:51:16 +00:00
|
|
|
conn.put_file(utils.path_dwim(self.basedir, source), temppath)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
|
|
|
# install the template module
|
2012-04-04 14:57:54 +00:00
|
|
|
template_module = self._transfer_module(conn, tmp, 'template')
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-04-04 11:38:21 +00:00
|
|
|
# transfer module vars
|
|
|
|
if self.module_vars:
|
|
|
|
vars = utils.bigjson(self.module_vars)
|
|
|
|
vars_path = self._transfer_str(conn, tmp, 'module_vars', vars)
|
|
|
|
vars_arg=" vars=%s"%(vars_path)
|
|
|
|
else:
|
|
|
|
vars_arg=""
|
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
# run the template module
|
2012-04-04 11:38:21 +00:00
|
|
|
args = "src=%s dest=%s metadata=%s%s" % (temppath, dest, metadata, vars_arg)
|
2012-03-30 20:17:16 +00:00
|
|
|
(result1, err, executed) = self._execute_module(conn, tmp, template_module, args)
|
|
|
|
(host, ok, data, err) = self._return_from_module(conn, host, result1, err, executed)
|
2012-03-16 02:32:14 +00:00
|
|
|
|
|
|
|
if ok:
|
2012-03-30 20:17:16 +00:00
|
|
|
return self._chain_file_module(conn, tmp, data, err, options, executed)
|
2012-03-16 02:32:14 +00:00
|
|
|
else:
|
2012-03-30 20:17:16 +00:00
|
|
|
return (host, ok, data, err)
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
2012-02-25 22:16:23 +00:00
|
|
|
|
|
|
|
def _executor(self, host):
|
2012-03-25 23:05:27 +00:00
|
|
|
try:
|
2012-03-30 20:17:16 +00:00
|
|
|
(host, ok, data, err) = self._executor_internal(host)
|
2012-03-27 01:17:11 +00:00
|
|
|
if not ok:
|
2012-03-27 01:25:43 +00:00
|
|
|
self.callbacks.on_unreachable(host, data)
|
2012-03-27 01:17:11 +00:00
|
|
|
return (host, ok, data)
|
2012-03-25 23:05:27 +00:00
|
|
|
except errors.AnsibleError, ae:
|
|
|
|
msg = str(ae)
|
|
|
|
self.callbacks.on_unreachable(host, msg)
|
|
|
|
return [host, False, msg]
|
|
|
|
except Exception:
|
|
|
|
msg = traceback.format_exc()
|
|
|
|
self.callbacks.on_unreachable(host, msg)
|
|
|
|
return [host, False, msg]
|
|
|
|
|
|
|
|
def _executor_internal(self, host):
|
2012-03-22 03:39:09 +00:00
|
|
|
''' callback executed in parallel for each host. returns (hostname, connected_ok, extra) '''
|
2012-03-20 02:42:31 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
ok, conn = self._connect(host)
|
2012-03-03 03:54:25 +00:00
|
|
|
if not ok:
|
2012-03-30 20:17:16 +00:00
|
|
|
return [ host, False, conn , None]
|
2012-03-20 02:42:31 +00:00
|
|
|
|
|
|
|
cache = self.setup_cache.get(host, {})
|
|
|
|
module_name = utils.template(self.module_name, cache)
|
2012-03-09 05:19:55 +00:00
|
|
|
|
2012-03-03 03:38:55 +00:00
|
|
|
tmp = self._get_tmp_path(conn)
|
|
|
|
result = None
|
2012-03-29 02:51:16 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
if self.module_name == 'copy':
|
2012-03-03 03:38:55 +00:00
|
|
|
result = self._execute_copy(conn, host, tmp)
|
2012-02-25 22:16:23 +00:00
|
|
|
elif self.module_name == 'template':
|
2012-03-03 03:38:55 +00:00
|
|
|
result = self._execute_template(conn, host, tmp)
|
2012-02-25 22:16:23 +00:00
|
|
|
else:
|
2012-03-22 03:39:09 +00:00
|
|
|
if self.background == 0:
|
|
|
|
result = self._execute_normal_module(conn, host, tmp, module_name)
|
|
|
|
else:
|
|
|
|
result = self._execute_async_module(conn, host, tmp, module_name)
|
2012-03-11 22:40:35 +00:00
|
|
|
|
2012-03-03 03:38:55 +00:00
|
|
|
self._delete_remote_files(conn, tmp)
|
|
|
|
conn.close()
|
2012-03-25 23:05:27 +00:00
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
(host, connect_ok, data, err) = result
|
2012-03-25 23:05:27 +00:00
|
|
|
if not connect_ok:
|
|
|
|
self.callbacks.on_unreachable(host, data)
|
|
|
|
else:
|
|
|
|
if 'failed' in data or 'rc' in data and str(data['rc']) != '0':
|
|
|
|
self.callbacks.on_failed(host, data)
|
|
|
|
elif 'skipped' in data:
|
|
|
|
self.callbacks.on_skipped(host)
|
|
|
|
else:
|
|
|
|
self.callbacks.on_ok(host, data)
|
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
if self.debug and err:
|
|
|
|
self.callbacks.on_error(host, err)
|
|
|
|
|
2012-03-03 03:38:55 +00:00
|
|
|
return result
|
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-30 02:58:10 +00:00
|
|
|
def _exec_command(self, conn, cmd, tmp, sudoable=False):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' execute a command string over SSH, return the output '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
msg = '%s: %s' % (self.module_name, cmd)
|
2012-03-15 00:00:09 +00:00
|
|
|
# log remote command execution
|
2012-03-30 02:58:10 +00:00
|
|
|
conn.exec_command('/usr/bin/logger -t ansible -p auth.info "%s"' % msg, None)
|
2012-03-15 00:00:09 +00:00
|
|
|
# now run actual command
|
2012-03-30 02:58:10 +00:00
|
|
|
stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudoable=sudoable)
|
2012-03-30 20:17:16 +00:00
|
|
|
|
|
|
|
if type(stderr) != str:
|
|
|
|
err="\n".join(stderr.readlines())
|
|
|
|
else:
|
|
|
|
err=stderr
|
|
|
|
|
2012-03-29 02:51:16 +00:00
|
|
|
if type(stdout) != str:
|
2012-03-30 20:17:16 +00:00
|
|
|
return "\n".join(stdout.readlines()), err
|
2012-03-29 02:51:16 +00:00
|
|
|
else:
|
2012-03-30 20:17:16 +00:00
|
|
|
return stdout, err
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-02-27 22:52:37 +00:00
|
|
|
def _get_tmp_path(self, conn):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' gets a temporary path on a remote box '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-30 20:17:16 +00:00
|
|
|
result, err = self._exec_command(conn, "mktemp -d /tmp/ansible.XXXXXX", None, sudoable=False)
|
2012-03-29 02:51:16 +00:00
|
|
|
cleaned = result.split("\n")[0].strip() + '/'
|
|
|
|
return cleaned
|
|
|
|
|
2012-02-27 22:52:37 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
def _copy_module(self, conn, tmp, module):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' transfer a module over SFTP, does not run it '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-13 00:53:10 +00:00
|
|
|
if module.startswith("/"):
|
2012-03-18 21:16:12 +00:00
|
|
|
raise errors.AnsibleFileNotFound("%s is not a module" % module)
|
2012-03-14 00:59:05 +00:00
|
|
|
in_path = os.path.expanduser(os.path.join(self.module_path, module))
|
2012-03-13 00:53:10 +00:00
|
|
|
if not os.path.exists(in_path):
|
2012-03-18 21:16:12 +00:00
|
|
|
raise errors.AnsibleFileNotFound("module not found: %s" % in_path)
|
2012-03-13 00:53:10 +00:00
|
|
|
|
2012-03-03 17:25:56 +00:00
|
|
|
out_path = tmp + module
|
2012-03-10 18:35:46 +00:00
|
|
|
conn.put_file(in_path, out_path)
|
2012-02-25 22:16:23 +00:00
|
|
|
return out_path
|
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def _match_hosts(self, pattern):
|
2012-02-27 01:29:27 +00:00
|
|
|
''' return all matched hosts fitting a pattern '''
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2012-03-31 02:47:58 +00:00
|
|
|
return [ h for h in self.host_list if self._matches(h, pattern) ]
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-14 00:59:05 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
def _parallel_exec(self, hosts):
|
|
|
|
''' handles mulitprocessing when more than 1 fork is required '''
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
job_queue = multiprocessing.Manager().Queue()
|
|
|
|
[job_queue.put(i) for i in hosts]
|
|
|
|
|
2012-03-25 23:05:27 +00:00
|
|
|
result_queue = multiprocessing.Manager().Queue()
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
workers = []
|
|
|
|
for i in range(self.forks):
|
|
|
|
prc = multiprocessing.Process(target=_executor_hook,
|
|
|
|
args=(job_queue, result_queue))
|
|
|
|
prc.start()
|
|
|
|
workers.append(prc)
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2012-04-04 00:20:55 +00:00
|
|
|
try:
|
|
|
|
for worker in workers:
|
|
|
|
worker.join()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
for worker in workers:
|
|
|
|
worker.terminate()
|
|
|
|
worker.join()
|
2012-02-25 22:16:23 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
results = []
|
|
|
|
while not result_queue.empty():
|
|
|
|
results.append(result_queue.get(block=False))
|
|
|
|
return results
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _partition_results(self, results):
|
|
|
|
''' seperate results by ones we contacted & ones we didn't '''
|
|
|
|
|
|
|
|
results2 = dict(contacted={}, dark={})
|
|
|
|
|
2012-03-25 23:05:27 +00:00
|
|
|
if results is None:
|
|
|
|
return None
|
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
for result in results:
|
|
|
|
(host, contacted_ok, result) = result
|
|
|
|
if contacted_ok:
|
2012-02-25 22:16:23 +00:00
|
|
|
results2["contacted"][host] = result
|
2012-03-22 03:39:09 +00:00
|
|
|
else:
|
|
|
|
results2["dark"][host] = result
|
|
|
|
|
|
|
|
# hosts which were contacted but never got a chance to return
|
|
|
|
for host in self._match_hosts(self.pattern):
|
|
|
|
if not (host in results2['dark'] or host in results2['contacted']):
|
2012-02-27 05:43:02 +00:00
|
|
|
results2["dark"][host] = {}
|
2012-03-22 03:39:09 +00:00
|
|
|
|
2012-02-25 22:16:23 +00:00
|
|
|
return results2
|
2012-02-24 04:28:58 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
''' xfer & run module on all matched hosts '''
|
|
|
|
|
|
|
|
# find hosts that match the pattern
|
|
|
|
hosts = self._match_hosts(self.pattern)
|
|
|
|
if len(hosts) == 0:
|
|
|
|
return dict(contacted={}, dark={})
|
2012-03-22 04:30:05 +00:00
|
|
|
|
2012-03-22 03:39:09 +00:00
|
|
|
hosts = [ (self,x) for x in hosts ]
|
2012-03-25 23:05:27 +00:00
|
|
|
results = None
|
2012-03-22 03:39:09 +00:00
|
|
|
if self.forks > 1:
|
2012-03-22 03:47:58 +00:00
|
|
|
results = self._parallel_exec(hosts)
|
2012-03-22 03:39:09 +00:00
|
|
|
else:
|
2012-03-25 23:05:27 +00:00
|
|
|
results = [ self._executor(h[1]) for h in hosts ]
|
2012-03-22 03:39:09 +00:00
|
|
|
return self._partition_results(results)
|
|
|
|
|
2012-02-24 04:28:58 +00:00
|
|
|
|