2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 05:28:58 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01: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-03-03 04:03:03 +01:00
|
|
|
|
2012-02-24 05:28:58 +01:00
|
|
|
import multiprocessing
|
2012-02-27 06:43:02 +01:00
|
|
|
import signal
|
2012-02-24 05:28:58 +01:00
|
|
|
import os
|
2012-04-11 01:27:19 +02:00
|
|
|
import pwd
|
2012-02-28 06:45:37 +01:00
|
|
|
import Queue
|
2012-03-03 18:25:56 +01:00
|
|
|
import random
|
2012-03-14 01:59:05 +01:00
|
|
|
import traceback
|
2012-03-14 05:34:00 +01:00
|
|
|
import tempfile
|
2012-04-26 20:34:49 +02:00
|
|
|
import time
|
2012-07-15 16:57:22 +02:00
|
|
|
import collections
|
2012-08-09 03:19:20 +02:00
|
|
|
import socket
|
2012-09-27 05:50:54 +02:00
|
|
|
import base64
|
2012-10-11 13:43:56 +02:00
|
|
|
import sys
|
2012-03-13 04:11:54 +01:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
import ansible.constants as C
|
2012-04-13 14:39:54 +02:00
|
|
|
import ansible.inventory
|
2012-03-18 22:04:07 +01:00
|
|
|
from ansible import utils
|
2012-03-18 22:16:12 +01:00
|
|
|
from ansible import errors
|
2012-07-18 04:33:36 +02:00
|
|
|
from ansible import module_common
|
2012-06-01 03:44:30 +02:00
|
|
|
import poller
|
|
|
|
import connection
|
2012-08-18 15:30:33 +02:00
|
|
|
from return_data import ReturnData
|
2012-08-10 06:25:13 +02:00
|
|
|
from ansible.callbacks import DefaultRunnerCallbacks, vv
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-29 02:32:04 +02:00
|
|
|
HAS_ATFORK=True
|
|
|
|
try:
|
|
|
|
from Crypto.Random import atfork
|
|
|
|
except ImportError:
|
|
|
|
HAS_ATFORK=False
|
2012-03-15 00:57:56 +01:00
|
|
|
|
2012-10-23 14:04:32 +02:00
|
|
|
multiprocessing_runner = None
|
2012-09-18 14:41:27 +02:00
|
|
|
|
2012-03-03 04:03:03 +01:00
|
|
|
################################################
|
2012-02-24 05:28:58 +01:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-10-27 00:11:38 +02:00
|
|
|
def _executor_hook(job_queue, result_queue):
|
|
|
|
|
2012-10-23 14:19:15 +02:00
|
|
|
# attempt workaround of https://github.com/newsapps/beeswithmachineguns/issues/17
|
|
|
|
# this function also not present in CentOS 6
|
|
|
|
if HAS_ATFORK:
|
|
|
|
atfork()
|
2012-10-27 00:11:38 +02:00
|
|
|
|
2012-10-24 13:40:31 +02:00
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
2012-10-27 00:11:38 +02:00
|
|
|
while not job_queue.empty():
|
|
|
|
try:
|
|
|
|
host = job_queue.get(block=False)
|
|
|
|
result_queue.put(multiprocessing_runner._executor(host))
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2012-10-23 14:19:15 +02:00
|
|
|
|
2012-10-08 20:26:58 +02:00
|
|
|
class HostVars(dict):
|
2012-10-13 02:07:05 +02:00
|
|
|
''' A special view of setup_cache that adds values from the inventory when needed. '''
|
|
|
|
|
2012-10-08 20:26:58 +02:00
|
|
|
def __init__(self, setup_cache, inventory):
|
|
|
|
self.setup_cache = setup_cache
|
|
|
|
self.inventory = inventory
|
|
|
|
self.lookup = {}
|
|
|
|
|
|
|
|
self.update(setup_cache)
|
|
|
|
|
|
|
|
def __getitem__(self, host):
|
|
|
|
if not host in self.lookup:
|
2012-10-13 02:07:05 +02:00
|
|
|
result = self.inventory.get_variables(host)
|
|
|
|
result.update(self.setup_cache.get(host, {}))
|
|
|
|
self.lookup[host] = result
|
|
|
|
return self.lookup[host]
|
2012-10-08 20:26:58 +02:00
|
|
|
|
2012-02-24 05:28:58 +01:00
|
|
|
class Runner(object):
|
2012-07-15 16:22:15 +02:00
|
|
|
''' core API interface to ansible '''
|
2012-02-24 05:28:58 +01:00
|
|
|
|
2012-07-21 22:51:31 +02:00
|
|
|
# see bin/ansible for how this is used...
|
2012-03-22 04:39:09 +01:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
def __init__(self,
|
2012-07-21 22:51:31 +02:00
|
|
|
host_list=C.DEFAULT_HOST_LIST, # ex: /etc/ansible/hosts, legacy usage
|
|
|
|
module_path=C.DEFAULT_MODULE_PATH, # ex: /usr/share/ansible
|
|
|
|
module_name=C.DEFAULT_MODULE_NAME, # ex: copy
|
|
|
|
module_args=C.DEFAULT_MODULE_ARGS, # ex: "src=/tmp/a dest=/tmp/b"
|
|
|
|
forks=C.DEFAULT_FORKS, # parallelism level
|
2012-07-24 02:06:18 +02:00
|
|
|
timeout=C.DEFAULT_TIMEOUT, # SSH timeout
|
2012-07-21 22:51:31 +02:00
|
|
|
pattern=C.DEFAULT_PATTERN, # which hosts? ex: 'all', 'acme.example.org'
|
|
|
|
remote_user=C.DEFAULT_REMOTE_USER, # ex: 'username'
|
|
|
|
remote_pass=C.DEFAULT_REMOTE_PASS, # ex: 'password123' or None if using key
|
2012-10-09 01:03:37 +02:00
|
|
|
remote_port=None, # if SSH on different ports
|
2012-08-07 02:07:02 +02:00
|
|
|
private_key_file=C.DEFAULT_PRIVATE_KEY_FILE, # if not using keys/passwords
|
2012-07-21 22:51:31 +02:00
|
|
|
sudo_pass=C.DEFAULT_SUDO_PASS, # ex: 'password123' or None
|
|
|
|
background=0, # async poll every X seconds, else 0 for non-async
|
|
|
|
basedir=None, # directory of playbook, if applicable
|
|
|
|
setup_cache=None, # used to share fact data w/ other tasks
|
|
|
|
transport=C.DEFAULT_TRANSPORT, # 'ssh', 'paramiko', 'local'
|
|
|
|
conditional='True', # run only if this fact expression evals to true
|
|
|
|
callbacks=None, # used for output
|
|
|
|
sudo=False, # whether to run sudo or not
|
|
|
|
sudo_user=C.DEFAULT_SUDO_USER, # ex: 'root'
|
2012-08-07 02:07:02 +02:00
|
|
|
module_vars=None, # a playbooks internals thing
|
2012-07-21 22:51:31 +02:00
|
|
|
is_playbook=False, # running from playbook or not?
|
2012-08-11 20:05:24 +02:00
|
|
|
inventory=None, # reference to Inventory object
|
|
|
|
subset=None # subset pattern
|
2012-07-21 22:51:31 +02:00
|
|
|
):
|
|
|
|
|
|
|
|
# storage & defaults
|
|
|
|
self.setup_cache = utils.default(setup_cache, lambda: collections.defaultdict(dict))
|
|
|
|
self.basedir = utils.default(basedir, lambda: os.getcwd())
|
2012-08-09 03:13:31 +02:00
|
|
|
self.callbacks = utils.default(callbacks, lambda: DefaultRunnerCallbacks())
|
2012-07-21 22:51:31 +02:00
|
|
|
self.generated_jid = str(random.randint(0, 999999999999))
|
|
|
|
self.transport = transport
|
|
|
|
self.inventory = utils.default(inventory, lambda: ansible.inventory.Inventory(host_list))
|
2012-08-11 20:05:24 +02:00
|
|
|
|
2012-07-21 22:51:31 +02:00
|
|
|
self.module_vars = utils.default(module_vars, lambda: {})
|
2012-07-15 17:54:39 +02:00
|
|
|
self.sudo_user = sudo_user
|
|
|
|
self.connector = connection.Connection(self)
|
|
|
|
self.conditional = conditional
|
|
|
|
self.module_path = module_path
|
|
|
|
self.module_name = module_name
|
|
|
|
self.forks = int(forks)
|
|
|
|
self.pattern = pattern
|
|
|
|
self.module_args = module_args
|
|
|
|
self.timeout = timeout
|
|
|
|
self.remote_user = remote_user
|
|
|
|
self.remote_pass = remote_pass
|
|
|
|
self.remote_port = remote_port
|
2012-05-14 22:14:38 +02:00
|
|
|
self.private_key_file = private_key_file
|
2012-07-15 17:54:39 +02:00
|
|
|
self.background = background
|
|
|
|
self.sudo = sudo
|
|
|
|
self.sudo_pass = sudo_pass
|
|
|
|
self.is_playbook = is_playbook
|
2012-03-31 04:47:58 +02:00
|
|
|
|
2012-07-21 22:51:31 +02:00
|
|
|
# misc housekeeping
|
2012-08-11 20:05:24 +02:00
|
|
|
if subset and self.inventory._subset is None:
|
|
|
|
# don't override subset when passed from playbook
|
|
|
|
self.inventory.subset(subset)
|
|
|
|
|
2012-07-21 22:51:31 +02:00
|
|
|
if self.transport == 'ssh' and remote_pass:
|
|
|
|
raise errors.AnsibleError("SSH transport does not support passwords, only keys or agents")
|
|
|
|
if self.transport == 'local':
|
|
|
|
self.remote_user = pwd.getpwuid(os.geteuid())[0]
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-21 22:51:31 +02:00
|
|
|
# ensure we are using unique tmp paths
|
2012-03-03 18:25:56 +01:00
|
|
|
random.seed()
|
2012-03-22 04:39:09 +01:00
|
|
|
|
2012-03-14 01:59:05 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-04-19 04:19:25 +02:00
|
|
|
def _transfer_str(self, conn, tmp, name, data):
|
|
|
|
''' transfer string to remote file '''
|
2012-03-21 00:55:04 +01:00
|
|
|
|
2012-04-19 04:19:25 +02:00
|
|
|
if type(data) == dict:
|
2012-07-15 16:12:49 +02:00
|
|
|
data = utils.jsonify(data)
|
2012-03-31 04:28:10 +02:00
|
|
|
|
2012-04-19 04:19:25 +02:00
|
|
|
afd, afile = tempfile.mkstemp()
|
|
|
|
afo = os.fdopen(afd, 'w')
|
2012-10-13 02:07:05 +02:00
|
|
|
try:
|
|
|
|
afo.write(data.encode('utf8'))
|
|
|
|
except:
|
|
|
|
raise errors.AnsibleError("failure encoding into utf-8")
|
2012-04-19 04:19:25 +02:00
|
|
|
afo.flush()
|
|
|
|
afo.close()
|
2012-03-31 04:28:10 +02:00
|
|
|
|
2012-04-19 04:19:25 +02:00
|
|
|
remote = os.path.join(tmp, name)
|
2012-07-15 18:29:53 +02:00
|
|
|
try:
|
|
|
|
conn.put_file(afile, remote)
|
|
|
|
finally:
|
|
|
|
os.unlink(afile)
|
2012-04-19 04:19:25 +02:00
|
|
|
return remote
|
2012-03-06 04:23:56 +01:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
# *****************************************************
|
2012-07-15 01:18:33 +02:00
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
def _execute_module(self, conn, tmp, module_name, args,
|
2012-07-15 20:19:11 +02:00
|
|
|
async_jid=None, async_module=None, async_limit=None, inject=None):
|
2012-07-15 01:18:33 +02:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
''' runs a module that has already been transferred '''
|
|
|
|
|
2012-09-27 05:50:54 +02:00
|
|
|
# hack to support fireball mode
|
|
|
|
if module_name == 'fireball':
|
2012-10-09 01:03:37 +02:00
|
|
|
args = "%s password=%s" % (args, base64.b64encode(str(utils.key_for_hostname(conn.host))))
|
|
|
|
if 'port' not in args:
|
|
|
|
args += " port=%s" % C.ZEROMQ_PORT
|
2012-09-27 05:50:54 +02:00
|
|
|
|
2012-10-21 04:51:36 +02:00
|
|
|
(remote_module_path, is_new_style, shebang) = self._copy_module(conn, tmp, module_name, args, inject)
|
|
|
|
|
|
|
|
cmd_mod = ""
|
2012-07-28 14:48:05 +02:00
|
|
|
if self.sudo and self.sudo_user != 'root':
|
|
|
|
# deal with possible umask issues once sudo'ed to other user
|
2012-10-24 17:30:49 +02:00
|
|
|
cmd_chmod = "chmod a+r %s" % remote_module_path
|
|
|
|
self._low_level_exec_command(conn, cmd_chmod, tmp, sudoable=False)
|
2012-04-11 02:58:40 +02:00
|
|
|
|
2012-07-24 01:14:37 +02:00
|
|
|
cmd = ""
|
|
|
|
if not is_new_style:
|
2012-09-17 14:02:30 +02:00
|
|
|
args = utils.template(self.basedir, args, inject)
|
2012-07-24 01:14:37 +02:00
|
|
|
argsfile = self._transfer_str(conn, tmp, 'arguments', args)
|
|
|
|
if async_jid is None:
|
|
|
|
cmd = "%s %s" % (remote_module_path, argsfile)
|
|
|
|
else:
|
|
|
|
cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module, argsfile]])
|
2012-03-15 00:57:56 +01:00
|
|
|
else:
|
2012-07-24 01:14:37 +02:00
|
|
|
if async_jid is None:
|
|
|
|
cmd = "%s" % (remote_module_path)
|
|
|
|
else:
|
|
|
|
cmd = " ".join([str(x) for x in [remote_module_path, async_jid, async_limit, async_module]])
|
2012-04-02 19:29:12 +02:00
|
|
|
|
2012-10-21 04:51:36 +02:00
|
|
|
if not shebang:
|
|
|
|
raise errors.AnsibleError("module is missing interpreter line")
|
|
|
|
|
|
|
|
cmd = shebang.replace("#!","") + " " + cmd
|
|
|
|
if tmp.find("tmp") != -1:
|
2012-10-23 14:04:32 +02:00
|
|
|
cmd = cmd + "; rm -rf %s >/dev/null 2>&1" % tmp
|
2012-05-26 00:44:29 +02:00
|
|
|
res = self._low_level_exec_command(conn, cmd, tmp, sudoable=True)
|
2012-08-18 14:46:51 +02:00
|
|
|
return ReturnData(conn=conn, result=res)
|
2012-03-22 04:39:09 +01:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2012-02-25 23:16:23 +01:00
|
|
|
def _executor(self, host):
|
2012-07-15 16:57:22 +02:00
|
|
|
''' handler for multiprocessing library '''
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
try:
|
2012-05-26 00:44:29 +02:00
|
|
|
exec_rc = self._executor_internal(host)
|
|
|
|
if type(exec_rc) != ReturnData:
|
|
|
|
raise Exception("unexpected return type: %s" % type(exec_rc))
|
2012-07-20 14:56:33 +02:00
|
|
|
# redundant, right?
|
2012-07-20 17:15:57 +02:00
|
|
|
if not exec_rc.comm_ok:
|
|
|
|
self.callbacks.on_unreachable(host, exec_rc.result)
|
2012-05-26 00:44:29 +02:00
|
|
|
return exec_rc
|
2012-03-26 01:05:27 +02:00
|
|
|
except errors.AnsibleError, ae:
|
|
|
|
msg = str(ae)
|
|
|
|
self.callbacks.on_unreachable(host, msg)
|
2012-05-26 00:44:29 +02:00
|
|
|
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg))
|
2012-03-26 01:05:27 +02:00
|
|
|
except Exception:
|
|
|
|
msg = traceback.format_exc()
|
|
|
|
self.callbacks.on_unreachable(host, msg)
|
2012-05-26 00:44:29 +02:00
|
|
|
return ReturnData(host=host, comm_ok=False, result=dict(failed=True, msg=msg))
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-07-15 16:22:15 +02:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
def _executor_internal(self, host):
|
2012-07-15 18:51:56 +02:00
|
|
|
''' executes any module one or more times '''
|
|
|
|
|
2012-07-18 02:08:45 +02:00
|
|
|
host_variables = self.inventory.get_variables(host)
|
2012-10-09 01:03:37 +02:00
|
|
|
if self.transport in [ 'paramiko', 'ssh' ]:
|
|
|
|
port = host_variables.get('ansible_ssh_port', self.remote_port)
|
|
|
|
if port is None:
|
2012-10-19 02:14:15 +02:00
|
|
|
port = C.DEFAULT_REMOTE_PORT
|
2012-10-09 01:03:37 +02:00
|
|
|
else:
|
|
|
|
# fireball, local, etc
|
|
|
|
port = self.remote_port
|
2012-07-21 22:23:00 +02:00
|
|
|
|
2012-08-19 01:49:49 +02:00
|
|
|
inject = {}
|
2012-07-18 02:08:45 +02:00
|
|
|
inject.update(host_variables)
|
|
|
|
inject.update(self.module_vars)
|
2012-08-19 01:49:49 +02:00
|
|
|
inject.update(self.setup_cache[host])
|
2012-10-08 20:26:58 +02:00
|
|
|
inject['hostvars'] = HostVars(self.setup_cache, self.inventory)
|
2012-09-08 00:07:52 +02:00
|
|
|
inject['group_names'] = host_variables.get('group_names', [])
|
|
|
|
inject['groups'] = self.inventory.groups_list()
|
2012-07-20 14:29:44 +02:00
|
|
|
|
2012-10-25 15:10:33 +02:00
|
|
|
# allow with_foo to work in playbooks...
|
2012-10-31 16:37:26 +01:00
|
|
|
items = None
|
2012-10-13 02:07:05 +02:00
|
|
|
items_plugin = self.module_vars.get('items_lookup_plugin', None)
|
2012-11-02 00:41:50 +01:00
|
|
|
if items_plugin is not None and items_plugin in utils.plugins.lookup_loader:
|
2012-10-13 02:07:05 +02:00
|
|
|
items_terms = self.module_vars.get('items_lookup_terms', '')
|
2012-10-31 16:37:26 +01:00
|
|
|
items_terms = utils.varReplaceWithItems(self.basedir, items_terms, inject)
|
2012-11-02 00:41:50 +01:00
|
|
|
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=self.basedir).run(items_terms, inject=inject)
|
2012-10-31 16:37:26 +01:00
|
|
|
if type(items) != list:
|
|
|
|
raise errors.AnsibleError("lookup plugins have to return a list: %r" % items)
|
2012-10-13 02:07:05 +02:00
|
|
|
|
2012-10-31 16:37:26 +01:00
|
|
|
if len(items) and self.module_name in [ 'apt', 'yum' ]:
|
|
|
|
# hack for apt and soon yum, with_items maps back into a single module call
|
|
|
|
inject['item'] = ",".join(items)
|
|
|
|
items = None
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-08-18 14:46:51 +02:00
|
|
|
# logic to decide how to run things depends on whether with_items is used
|
|
|
|
|
2012-10-31 16:37:26 +01:00
|
|
|
if items is None:
|
2012-09-21 10:42:27 +02:00
|
|
|
return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port)
|
2012-10-31 16:37:26 +01:00
|
|
|
elif len(items) > 0:
|
2012-07-15 18:51:56 +02:00
|
|
|
# executing using with_items, so make multiple calls
|
|
|
|
# TODO: refactor
|
|
|
|
aggregrate = {}
|
|
|
|
all_comm_ok = True
|
|
|
|
all_changed = False
|
|
|
|
all_failed = False
|
|
|
|
results = []
|
|
|
|
for x in items:
|
2012-07-18 02:08:45 +02:00
|
|
|
inject['item'] = x
|
2012-09-21 10:42:27 +02:00
|
|
|
result = self._executor_internal_inner(host, self.module_name, self.module_args, inject, port)
|
2012-07-15 18:51:56 +02:00
|
|
|
results.append(result.result)
|
|
|
|
if result.comm_ok == False:
|
|
|
|
all_comm_ok = False
|
2012-10-13 02:07:05 +02:00
|
|
|
all_failed = True
|
2012-07-15 18:51:56 +02:00
|
|
|
break
|
|
|
|
for x in results:
|
|
|
|
if x.get('changed') == True:
|
|
|
|
all_changed = True
|
|
|
|
if (x.get('failed') == True) or (('rc' in x) and (x['rc'] != 0)):
|
|
|
|
all_failed = True
|
|
|
|
break
|
2012-10-13 02:07:05 +02:00
|
|
|
msg = 'All items completed'
|
2012-07-15 18:51:56 +02:00
|
|
|
if all_failed:
|
|
|
|
msg = "One or more items failed."
|
|
|
|
rd_result = dict(failed=all_failed, changed=all_changed, results=results, msg=msg)
|
|
|
|
if not all_failed:
|
|
|
|
del rd_result['failed']
|
|
|
|
return ReturnData(host=host, comm_ok=all_comm_ok, result=rd_result)
|
2012-10-31 16:37:26 +01:00
|
|
|
else:
|
|
|
|
self.callbacks.on_skipped(host, None)
|
|
|
|
return ReturnData(host=host, comm_ok=True, result=dict(skipped=True))
|
2012-07-15 18:51:56 +02:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
def _executor_internal_inner(self, host, module_name, module_args, inject, port, is_chained=False):
|
2012-07-15 18:51:56 +02:00
|
|
|
''' decides how to invoke a module '''
|
2012-03-20 03:42:31 +01:00
|
|
|
|
2012-07-15 20:19:11 +02:00
|
|
|
# special non-user/non-fact variables:
|
|
|
|
# 'groups' variable is a list of host name in each group
|
|
|
|
# 'hostvars' variable contains variables for each host name
|
|
|
|
# ... and is set elsewhere
|
|
|
|
# 'inventory_hostname' is also set elsewhere
|
2012-09-08 00:07:52 +02:00
|
|
|
inject['groups'] = self.inventory.groups_list()
|
2012-09-21 10:42:27 +02:00
|
|
|
|
2012-07-22 17:08:16 +02:00
|
|
|
# allow module args to work as a dictionary
|
|
|
|
# though it is usually a string
|
|
|
|
new_args = ""
|
2012-09-21 10:42:27 +02:00
|
|
|
if type(module_args) == dict:
|
|
|
|
for (k,v) in module_args.iteritems():
|
2012-07-22 17:08:16 +02:00
|
|
|
new_args = new_args + "%s='%s' " % (k,v)
|
2012-09-21 10:42:27 +02:00
|
|
|
module_args = new_args
|
2012-07-22 17:08:16 +02:00
|
|
|
|
2012-09-28 13:58:23 +02:00
|
|
|
conditional = utils.template(self.basedir, self.conditional, inject)
|
2012-09-24 21:06:34 +02:00
|
|
|
if not utils.check_conditional(conditional):
|
2012-07-15 16:12:49 +02:00
|
|
|
result = utils.jsonify(dict(skipped=True))
|
2012-07-24 01:36:26 +02:00
|
|
|
self.callbacks.on_skipped(host, inject.get('item',None))
|
2012-06-11 15:06:23 +02:00
|
|
|
return ReturnData(host=host, result=result)
|
|
|
|
|
2012-04-17 03:52:15 +02:00
|
|
|
conn = None
|
2012-10-18 01:17:01 +02:00
|
|
|
actual_host = inject.get('ansible_ssh_host', host)
|
2012-10-18 04:50:17 +02:00
|
|
|
actual_port = port
|
2012-10-31 01:42:07 +01:00
|
|
|
if self.transport in [ 'paramiko', 'ssh' ]:
|
2012-10-18 04:50:17 +02:00
|
|
|
actual_port = inject.get('ansible_ssh_port', port)
|
2012-10-18 01:17:01 +02:00
|
|
|
|
|
|
|
# the delegated host may have different SSH port configured, etc
|
|
|
|
# and we need to transfer those, and only those, variables
|
|
|
|
delegate_to = inject.get('delegate_to', None)
|
|
|
|
if delegate_to is not None:
|
|
|
|
delegate_to = utils.template(self.basedir, delegate_to, inject)
|
2012-10-25 13:59:24 +02:00
|
|
|
inject = inject.copy()
|
|
|
|
interpreters = []
|
|
|
|
for i in inject:
|
|
|
|
if i.startswith("ansible_") and i.endswith("_interpreter"):
|
|
|
|
interpreters.append(i)
|
|
|
|
for i in interpreters:
|
|
|
|
del inject[i]
|
2012-11-01 11:08:24 +01:00
|
|
|
port = C.DEFAULT_REMOTE_PORT
|
2012-10-18 21:48:10 +02:00
|
|
|
try:
|
|
|
|
delegate_info = inject['hostvars'][delegate_to]
|
|
|
|
actual_host = delegate_info.get('ansible_ssh_host', delegate_to)
|
|
|
|
actual_port = delegate_info.get('ansible_ssh_port', port)
|
2012-10-25 13:59:24 +02:00
|
|
|
for i in delegate_info:
|
|
|
|
if i.startswith("ansible_") and i.endswith("_interpreter"):
|
|
|
|
inject[i] = delegate_info[i]
|
2012-10-18 21:48:10 +02:00
|
|
|
except errors.AnsibleError:
|
|
|
|
actual_host = delegate_to
|
2012-11-01 11:08:24 +01:00
|
|
|
actual_port = port
|
2012-10-17 02:57:37 +02:00
|
|
|
|
2012-10-18 01:17:01 +02:00
|
|
|
try:
|
2012-10-19 16:18:38 +02:00
|
|
|
if actual_port is not None:
|
|
|
|
actual_port = int(actual_port)
|
2012-10-20 16:53:34 +02:00
|
|
|
conn = self.connector.connect(actual_host, actual_port)
|
2012-10-21 20:11:27 +02:00
|
|
|
if delegate_to or host != actual_host:
|
2012-10-18 01:17:01 +02:00
|
|
|
conn.delegate = host
|
2012-10-17 02:57:37 +02:00
|
|
|
|
2012-10-19 02:14:15 +02:00
|
|
|
|
2012-04-17 03:52:15 +02:00
|
|
|
except errors.AnsibleConnectionFailed, e:
|
2012-05-26 00:44:29 +02:00
|
|
|
result = dict(failed=True, msg="FAILED: %s" % str(e))
|
|
|
|
return ReturnData(host=host, comm_ok=False, result=result)
|
2012-04-17 03:52:15 +02:00
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
module_name = utils.template(self.basedir, module_name, inject)
|
2012-10-18 01:35:12 +02:00
|
|
|
module_args = utils.template(self.basedir, module_args, inject, expand_lists=True)
|
2012-03-09 06:19:55 +01:00
|
|
|
|
2012-08-10 06:45:00 +02:00
|
|
|
tmp = ''
|
|
|
|
if self.module_name != 'raw':
|
|
|
|
tmp = self._make_tmp_path(conn)
|
2012-03-03 04:38:55 +01:00
|
|
|
result = None
|
2012-03-29 04:51:16 +02:00
|
|
|
|
2012-11-02 00:41:50 +01:00
|
|
|
if module_name in utils.plugins.action_loader:
|
2012-10-01 02:06:55 +02:00
|
|
|
if self.background != 0:
|
|
|
|
raise errors.AnsibleError("async mode is not supported with the %s module" % module_name)
|
2012-11-02 00:41:50 +01:00
|
|
|
handler = utils.plugins.action_loader.get(module_name, self)
|
2012-09-21 10:42:27 +02:00
|
|
|
result = handler.run(conn, tmp, module_name, module_args, inject)
|
2012-02-25 23:16:23 +01:00
|
|
|
else:
|
2012-03-22 04:39:09 +01:00
|
|
|
if self.background == 0:
|
2012-11-02 00:41:50 +01:00
|
|
|
result = utils.plugins.action_loader.get('normal', self).run(conn, tmp, module_name, module_args, inject)
|
2012-03-22 04:39:09 +01:00
|
|
|
else:
|
2012-11-02 00:41:50 +01:00
|
|
|
result = utils.plugins.action_loader.get('async', self).run(conn, tmp, module_name, module_args, inject)
|
2012-03-11 23:40:35 +01:00
|
|
|
|
2012-03-03 04:38:55 +01:00
|
|
|
conn.close()
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-05-26 00:44:29 +02:00
|
|
|
if not result.comm_ok:
|
|
|
|
# connection or parsing errors...
|
2012-06-11 15:07:37 +02:00
|
|
|
self.callbacks.on_unreachable(host, result.result)
|
2012-03-26 01:05:27 +02:00
|
|
|
else:
|
2012-05-26 00:44:29 +02:00
|
|
|
data = result.result
|
2012-07-20 14:29:44 +02:00
|
|
|
if 'item' in inject:
|
|
|
|
result.result['item'] = inject['item']
|
2012-08-21 02:41:28 +02:00
|
|
|
|
|
|
|
result.result['invocation'] = dict(
|
2012-09-21 10:42:27 +02:00
|
|
|
module_args=module_args,
|
|
|
|
module_name=module_name
|
2012-08-21 02:41:28 +02:00
|
|
|
)
|
|
|
|
|
2012-07-24 04:12:26 +02:00
|
|
|
if is_chained:
|
|
|
|
# no callbacks
|
|
|
|
return result
|
|
|
|
if 'skipped' in data:
|
2012-08-18 14:46:51 +02:00
|
|
|
self.callbacks.on_skipped(host)
|
2012-07-24 04:12:26 +02:00
|
|
|
elif not result.is_successful():
|
2012-08-01 21:17:16 +02:00
|
|
|
ignore_errors = self.module_vars.get('ignore_errors', False)
|
2012-08-18 14:46:51 +02:00
|
|
|
self.callbacks.on_failed(host, data, ignore_errors)
|
2012-07-24 04:12:26 +02:00
|
|
|
else:
|
2012-08-18 14:46:51 +02:00
|
|
|
self.callbacks.on_ok(host, data)
|
2012-03-03 04:38:55 +01:00
|
|
|
return result
|
|
|
|
|
2012-03-14 01:59:05 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-05-26 00:44:29 +02:00
|
|
|
def _low_level_exec_command(self, conn, cmd, tmp, sudoable=False):
|
2012-02-27 02:29:27 +01:00
|
|
|
''' execute a command string over SSH, return the output '''
|
2012-07-15 16:57:22 +02:00
|
|
|
|
2012-05-04 02:11:21 +02:00
|
|
|
sudo_user = self.sudo_user
|
2012-05-07 00:24:04 +02:00
|
|
|
stdin, stdout, stderr = conn.exec_command(cmd, tmp, sudo_user, sudoable=sudoable)
|
2012-05-26 00:44:29 +02:00
|
|
|
|
2012-09-27 05:50:54 +02:00
|
|
|
if type(stdout) not in [ str, unicode ]:
|
2012-07-31 22:59:45 +02:00
|
|
|
out = "\n".join(stdout.readlines())
|
2012-03-29 04:51:16 +02:00
|
|
|
else:
|
2012-07-31 22:59:45 +02:00
|
|
|
out = stdout
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-09-27 05:50:54 +02:00
|
|
|
if type(stderr) not in [ str, unicode ]:
|
2012-07-31 22:59:45 +02:00
|
|
|
err = "\n".join(stderr.readlines())
|
|
|
|
else:
|
|
|
|
err = stderr
|
|
|
|
|
|
|
|
return out + err
|
2012-04-27 07:25:38 +02:00
|
|
|
|
2012-03-14 01:59:05 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-07-07 14:45:06 +02:00
|
|
|
def _remote_md5(self, conn, tmp, path):
|
2012-08-07 02:07:02 +02:00
|
|
|
''' takes a remote md5sum without requiring python, and returns 0 if no file '''
|
|
|
|
|
2012-08-10 15:11:54 +02:00
|
|
|
test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1" % (path,path)
|
2012-07-07 14:45:06 +02:00
|
|
|
md5s = [
|
2012-07-17 10:14:16 +02:00
|
|
|
"(/usr/bin/md5sum %s 2>/dev/null)" % path,
|
|
|
|
"(/sbin/md5sum -q %s 2>/dev/null)" % path,
|
|
|
|
"(/usr/bin/digest -a md5 -v %s 2>/dev/null)" % path
|
2012-07-07 14:45:06 +02:00
|
|
|
]
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-07 14:45:06 +02:00
|
|
|
cmd = " || ".join(md5s)
|
2012-07-17 10:14:16 +02:00
|
|
|
cmd = "%s; %s || (echo \"${rc} %s\")" % (test, cmd, path)
|
2012-08-17 04:45:43 +02:00
|
|
|
data = self._low_level_exec_command(conn, cmd, tmp, sudoable=False)
|
2012-10-11 13:43:56 +02:00
|
|
|
data2 = utils.last_non_blank_line(data)
|
|
|
|
try:
|
|
|
|
return data2.split()[0]
|
2012-10-11 13:56:01 +02:00
|
|
|
except IndexError:
|
2012-10-11 13:43:56 +02:00
|
|
|
sys.stderr.write("warning: md5sum command failed unusually, please report this to the list so it can be fixed\n")
|
|
|
|
sys.stderr.write("command: %s\n" % md5s)
|
|
|
|
sys.stderr.write("----\n")
|
|
|
|
sys.stderr.write("output: %s\n" % data)
|
|
|
|
sys.stderr.write("----\n")
|
|
|
|
# this will signal that it changed and allow things to keep going
|
|
|
|
return "INVALIDMD5SUM"
|
2012-07-07 14:45:06 +02:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2012-06-06 14:47:47 +02:00
|
|
|
def _make_tmp_path(self, conn):
|
|
|
|
''' make and return a temporary path on a remote box '''
|
2012-03-14 01:59:05 +01:00
|
|
|
|
2012-06-06 14:47:47 +02:00
|
|
|
basefile = 'ansible-%s-%s' % (time.time(), random.randint(0, 2**48))
|
|
|
|
basetmp = os.path.join(C.DEFAULT_REMOTE_TMP, basefile)
|
2012-05-10 07:50:22 +02:00
|
|
|
if self.remote_user == 'root':
|
2012-06-06 14:47:47 +02:00
|
|
|
basetmp = os.path.join('/var/tmp', basefile)
|
2012-07-28 02:53:50 +02:00
|
|
|
elif self.sudo and self.sudo_user != 'root':
|
|
|
|
basetmp = os.path.join('/tmp', basefile)
|
2012-06-06 14:47:47 +02:00
|
|
|
|
|
|
|
cmd = 'mkdir -p %s' % basetmp
|
2012-04-21 17:38:39 +02:00
|
|
|
if self.remote_user != 'root':
|
2012-08-08 07:40:22 +02:00
|
|
|
cmd += ' && chmod a+rx %s' % basetmp
|
2012-06-06 14:47:47 +02:00
|
|
|
cmd += ' && echo %s' % basetmp
|
2012-04-21 17:38:39 +02:00
|
|
|
|
2012-05-26 00:44:29 +02:00
|
|
|
result = self._low_level_exec_command(conn, cmd, None, sudoable=False)
|
2012-09-27 05:50:54 +02:00
|
|
|
rc = utils.last_non_blank_line(result).strip() + '/'
|
|
|
|
return rc
|
|
|
|
|
2012-02-27 23:52:37 +01:00
|
|
|
|
2012-03-14 01:59:05 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
def _copy_module(self, conn, tmp, module_name, module_args, inject):
|
2012-02-27 02:29:27 +01:00
|
|
|
''' transfer a module over SFTP, does not run it '''
|
2012-03-14 01:59:05 +01:00
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
if module_name.startswith("/"):
|
|
|
|
raise errors.AnsibleFileNotFound("%s is not a module" % module_name)
|
2012-06-06 22:42:29 +02:00
|
|
|
|
|
|
|
# Search module path(s) for named module.
|
|
|
|
for module_path in self.module_path.split(os.pathsep):
|
2012-09-21 10:42:27 +02:00
|
|
|
in_path = os.path.expanduser(os.path.join(module_path, module_name))
|
2012-06-06 22:42:29 +02:00
|
|
|
if os.path.exists(in_path):
|
|
|
|
break
|
|
|
|
else:
|
2012-09-21 10:42:27 +02:00
|
|
|
raise errors.AnsibleFileNotFound("module %s not found in %s" % (module_name, self.module_path))
|
2012-03-13 01:53:10 +01:00
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
out_path = os.path.join(tmp, module_name)
|
2012-06-04 10:15:12 +02:00
|
|
|
|
2012-07-18 04:33:36 +02:00
|
|
|
module_data = ""
|
2012-07-24 01:14:37 +02:00
|
|
|
is_new_style=False
|
2012-08-15 02:35:01 +02:00
|
|
|
|
2012-07-18 04:33:36 +02:00
|
|
|
with open(in_path) as f:
|
|
|
|
module_data = f.read()
|
2012-07-24 01:14:37 +02:00
|
|
|
if module_common.REPLACER in module_data:
|
|
|
|
is_new_style=True
|
2012-07-18 04:33:36 +02:00
|
|
|
module_data = module_data.replace(module_common.REPLACER, module_common.MODULE_COMMON)
|
2012-09-21 10:42:27 +02:00
|
|
|
encoded_args = "\"\"\"%s\"\"\"" % module_args.replace("\"","\\\"")
|
2012-07-24 01:14:37 +02:00
|
|
|
module_data = module_data.replace(module_common.REPLACER_ARGS, encoded_args)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-21 22:23:00 +02:00
|
|
|
# use the correct python interpreter for the host
|
|
|
|
if 'ansible_python_interpreter' in inject:
|
|
|
|
interpreter = inject['ansible_python_interpreter']
|
2012-07-18 04:33:36 +02:00
|
|
|
module_lines = module_data.split('\n')
|
2012-06-04 10:15:12 +02:00
|
|
|
if '#!' and 'python' in module_lines[0]:
|
|
|
|
module_lines[0] = "#!%s" % interpreter
|
2012-07-18 04:33:36 +02:00
|
|
|
module_data = "\n".join(module_lines)
|
2012-06-04 10:15:12 +02:00
|
|
|
|
2012-09-21 10:42:27 +02:00
|
|
|
self._transfer_str(conn, tmp, module_name, module_data)
|
2012-10-21 04:51:36 +02:00
|
|
|
|
|
|
|
lines = module_data.split("\n")
|
|
|
|
shebang = None
|
|
|
|
if lines[0].startswith("#!"):
|
|
|
|
shebang = lines[0]
|
|
|
|
|
|
|
|
return (out_path, is_new_style, shebang)
|
2012-02-25 23:16:23 +01:00
|
|
|
|
2012-03-14 01:59:05 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-10-27 00:11:38 +02:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
def _parallel_exec(self, hosts):
|
|
|
|
''' handles mulitprocessing when more than 1 fork is required '''
|
2012-04-04 16:27:24 +02:00
|
|
|
|
2012-10-27 00:11:38 +02:00
|
|
|
manager = multiprocessing.Manager()
|
|
|
|
job_queue = manager.Queue()
|
|
|
|
for host in hosts:
|
|
|
|
job_queue.put(host)
|
|
|
|
result_queue = manager.Queue()
|
|
|
|
|
|
|
|
workers = []
|
|
|
|
for i in range(self.forks):
|
|
|
|
prc = multiprocessing.Process(target=_executor_hook,
|
|
|
|
args=(job_queue, result_queue))
|
|
|
|
prc.start()
|
|
|
|
workers.append(prc)
|
2012-10-23 14:19:15 +02:00
|
|
|
|
2012-04-04 02:20:55 +02:00
|
|
|
try:
|
2012-10-27 00:11:38 +02:00
|
|
|
for worker in workers:
|
|
|
|
worker.join()
|
2012-04-04 02:20:55 +02:00
|
|
|
except KeyboardInterrupt:
|
2012-10-27 00:11:38 +02:00
|
|
|
for worker in workers:
|
|
|
|
worker.terminate()
|
|
|
|
worker.join()
|
|
|
|
|
|
|
|
results = []
|
|
|
|
try:
|
|
|
|
while not result_queue.empty():
|
|
|
|
results.append(result_queue.get(block=False))
|
|
|
|
except socket.error:
|
|
|
|
raise errors.AnsibleError("<interrupted>")
|
|
|
|
return results
|
2012-03-22 04:39:09 +01:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _partition_results(self, results):
|
|
|
|
''' seperate results by ones we contacted & ones we didn't '''
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
if results is None:
|
|
|
|
return None
|
2012-07-15 16:57:22 +02:00
|
|
|
results2 = dict(contacted={}, dark={})
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
for result in results:
|
2012-05-26 00:44:29 +02:00
|
|
|
host = result.host
|
|
|
|
if host is None:
|
|
|
|
raise Exception("internal error, host not set")
|
|
|
|
if result.communicated_ok():
|
|
|
|
results2["contacted"][host] = result.result
|
2012-03-22 04:39:09 +01:00
|
|
|
else:
|
2012-05-26 00:44:29 +02:00
|
|
|
results2["dark"][host] = result.result
|
2012-03-22 04:39:09 +01:00
|
|
|
|
|
|
|
# hosts which were contacted but never got a chance to return
|
2012-04-13 14:39:54 +02:00
|
|
|
for host in self.inventory.list_hosts(self.pattern):
|
2012-03-22 04:39:09 +01:00
|
|
|
if not (host in results2['dark'] or host in results2['contacted']):
|
2012-02-27 06:43:02 +01:00
|
|
|
results2["dark"][host] = {}
|
2012-02-25 23:16:23 +01:00
|
|
|
return results2
|
2012-02-24 05:28:58 +01:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
''' xfer & run module on all matched hosts '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-22 04:39:09 +01:00
|
|
|
# find hosts that match the pattern
|
2012-04-13 14:39:54 +02:00
|
|
|
hosts = self.inventory.list_hosts(self.pattern)
|
2012-03-22 04:39:09 +01:00
|
|
|
if len(hosts) == 0:
|
2012-04-12 03:05:46 +02:00
|
|
|
self.callbacks.on_no_hosts()
|
2012-03-22 04:39:09 +01:00
|
|
|
return dict(contacted={}, dark={})
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-10-23 14:04:32 +02:00
|
|
|
global multiprocessing_runner
|
|
|
|
multiprocessing_runner = self
|
2012-03-26 01:05:27 +02:00
|
|
|
results = None
|
2012-09-22 08:07:49 +02:00
|
|
|
|
|
|
|
# Check if this is an action plugin. Some of them are designed
|
|
|
|
# to be ran once per group of hosts. Example module: pause,
|
|
|
|
# run once per hostgroup, rather than pausing once per each
|
|
|
|
# host.
|
2012-11-02 00:41:50 +01:00
|
|
|
p = utils.plugins.action_loader.get(self.module_name, self)
|
2012-09-22 08:07:49 +02:00
|
|
|
if p and getattr(p, 'BYPASS_HOST_LOOP', None):
|
|
|
|
# Expose the current hostgroup to the bypassing plugins
|
|
|
|
self.host_set = hosts
|
|
|
|
# We aren't iterating over all the hosts in this
|
|
|
|
# group. So, just pick the first host in our group to
|
|
|
|
# construct the conn object with.
|
2012-10-29 09:45:44 +01:00
|
|
|
result_data = self._executor(hosts[0]).result
|
2012-09-22 08:07:49 +02:00
|
|
|
# Create a ResultData item for each host in this group
|
|
|
|
# using the returned result. If we didn't do this we would
|
|
|
|
# get false reports of dark hosts.
|
2012-10-23 14:04:32 +02:00
|
|
|
results = [ ReturnData(host=h, result=result_data, comm_ok=True) \
|
2012-09-22 08:07:49 +02:00
|
|
|
for h in hosts ]
|
|
|
|
del self.host_set
|
|
|
|
elif self.forks > 1:
|
2012-10-24 13:46:24 +02:00
|
|
|
try:
|
|
|
|
results = self._parallel_exec(hosts)
|
|
|
|
except IOError, ie:
|
|
|
|
print ie.errno
|
|
|
|
if ie.errno == 32:
|
|
|
|
# broken pipe from Ctrl+C
|
|
|
|
raise errors.AnsibleError("interupted")
|
|
|
|
raise
|
2012-03-22 04:39:09 +01:00
|
|
|
else:
|
2012-10-23 14:04:32 +02:00
|
|
|
results = [ self._executor(h) for h in hosts ]
|
2012-03-22 04:39:09 +01:00
|
|
|
return self._partition_results(results)
|
|
|
|
|
2012-07-15 16:22:15 +02:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-05-26 01:18:02 +02:00
|
|
|
def run_async(self, time_limit):
|
2012-04-26 20:34:49 +02:00
|
|
|
''' Run this module asynchronously and return a poller. '''
|
2012-07-15 16:57:22 +02:00
|
|
|
|
2012-04-26 20:34:49 +02:00
|
|
|
self.background = time_limit
|
|
|
|
results = self.run()
|
2012-05-26 01:18:02 +02:00
|
|
|
return results, poller.AsyncPoller(results, self)
|