2014-01-04 19:31:44 +01:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 05:26:16 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# This file is part of Ansible
|
2012-02-24 05:26:16 +01:00
|
|
|
#
|
2012-02-29 01:08:09 +01:00
|
|
|
# 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 05:26:16 +01:00
|
|
|
|
2012-04-13 14:39:54 +02:00
|
|
|
import ansible.inventory
|
2012-02-24 05:26:16 +01:00
|
|
|
import ansible.constants as C
|
2013-04-20 15:09:35 +02:00
|
|
|
import ansible.runner
|
2013-04-25 03:59:47 +02:00
|
|
|
from ansible.utils.template import template
|
2012-03-18 22:16:12 +01:00
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
2014-09-23 23:12:01 +02:00
|
|
|
from ansible.module_utils.splitter import split_args, unquote
|
2012-09-15 02:02:38 +02:00
|
|
|
import ansible.callbacks
|
2014-07-03 03:02:28 +02:00
|
|
|
import ansible.cache
|
2012-03-03 16:53:15 +01:00
|
|
|
import os
|
2012-11-09 13:09:20 +01:00
|
|
|
import shlex
|
2012-07-15 16:57:22 +02:00
|
|
|
import collections
|
2012-05-26 06:37:34 +02:00
|
|
|
from play import Play
|
2013-04-08 05:37:10 +02:00
|
|
|
import StringIO
|
|
|
|
import pipes
|
2012-03-06 02:09:03 +01:00
|
|
|
|
2014-04-01 16:48:14 +02:00
|
|
|
# the setup cache stores all variables about a host
|
|
|
|
# gathered during the setup step, while the vars cache
|
|
|
|
# holds all other variables about a host
|
2014-07-03 03:02:28 +02:00
|
|
|
SETUP_CACHE = ansible.cache.FactCache()
|
2014-04-01 16:48:14 +02:00
|
|
|
VARS_CACHE = collections.defaultdict(dict)
|
2015-02-09 04:51:18 +01:00
|
|
|
RESERVED_TAGS = ['all','tagged','untagged','always']
|
2012-07-26 02:45:51 +02:00
|
|
|
|
2014-07-03 03:02:28 +02:00
|
|
|
|
2012-02-24 05:26:16 +01:00
|
|
|
class PlayBook(object):
|
2012-03-03 04:03:03 +01:00
|
|
|
'''
|
2012-08-07 02:07:02 +02:00
|
|
|
runs an ansible playbook, given as a datastructure or YAML filename.
|
|
|
|
A playbook is a deployment, config management, or automation based
|
2012-07-15 17:20:59 +02:00
|
|
|
set of commands to run in series.
|
|
|
|
|
2012-08-07 02:07:02 +02:00
|
|
|
multiple plays/tasks do not execute simultaneously, but tasks in each
|
|
|
|
pattern do execute in parallel (according to the number of forks
|
2012-07-15 17:20:59 +02:00
|
|
|
requested) among the hosts they address
|
2012-02-24 05:26:16 +01:00
|
|
|
'''
|
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-03-03 04:03:03 +01:00
|
|
|
def __init__(self,
|
2012-03-26 01:05:27 +02:00
|
|
|
playbook = None,
|
|
|
|
host_list = C.DEFAULT_HOST_LIST,
|
2012-11-18 18:37:30 +01:00
|
|
|
module_path = None,
|
2012-03-26 01:05:27 +02:00
|
|
|
forks = C.DEFAULT_FORKS,
|
|
|
|
timeout = C.DEFAULT_TIMEOUT,
|
|
|
|
remote_user = C.DEFAULT_REMOTE_USER,
|
|
|
|
remote_pass = C.DEFAULT_REMOTE_PASS,
|
2012-10-18 04:50:17 +02:00
|
|
|
remote_port = None,
|
2012-04-11 18:39:04 +02:00
|
|
|
transport = C.DEFAULT_TRANSPORT,
|
2012-05-14 22:52:48 +02:00
|
|
|
private_key_file = C.DEFAULT_PRIVATE_KEY_FILE,
|
2012-03-26 01:05:27 +02:00
|
|
|
callbacks = None,
|
|
|
|
runner_callbacks = None,
|
2012-04-17 04:15:55 +02:00
|
|
|
stats = None,
|
2012-07-12 01:51:26 +02:00
|
|
|
extra_vars = None,
|
2012-08-10 08:45:29 +02:00
|
|
|
only_tags = None,
|
2013-06-25 07:33:29 +02:00
|
|
|
skip_tags = None,
|
2012-11-27 14:42:58 +01:00
|
|
|
subset = C.DEFAULT_SUBSET,
|
2013-02-04 01:46:25 +01:00
|
|
|
inventory = None,
|
2013-02-08 04:51:33 +01:00
|
|
|
check = False,
|
2013-03-28 07:17:01 +01:00
|
|
|
diff = False,
|
2014-01-21 02:19:03 +01:00
|
|
|
any_errors_fatal = False,
|
2014-02-11 18:03:11 +01:00
|
|
|
vault_password = False,
|
2014-03-24 15:28:48 +01:00
|
|
|
force_handlers = False,
|
2015-04-28 15:36:42 +02:00
|
|
|
# privilege escalation
|
2014-11-24 22:36:31 +01:00
|
|
|
become = C.DEFAULT_BECOME,
|
|
|
|
become_method = C.DEFAULT_BECOME_METHOD,
|
|
|
|
become_user = C.DEFAULT_BECOME_USER,
|
|
|
|
become_pass = None,
|
2014-01-21 02:19:03 +01:00
|
|
|
):
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-04-17 05:45:15 +02:00
|
|
|
"""
|
|
|
|
playbook: path to a playbook file
|
|
|
|
host_list: path to a file like /etc/ansible/hosts
|
|
|
|
module_path: path to ansible modules, like /usr/share/ansible/
|
2014-05-03 18:40:05 +02:00
|
|
|
forks: desired level of parallelism
|
2012-04-17 05:45:15 +02:00
|
|
|
timeout: connection timeout
|
|
|
|
remote_user: run as this user if not specified in a particular play
|
|
|
|
remote_pass: use this remote password (for all plays) vs using SSH keys
|
|
|
|
remote_port: default remote port to use if not specified with the host or play
|
|
|
|
transport: how to connect to hosts that don't specify a transport (local, paramiko, etc)
|
|
|
|
callbacks output callbacks for the playbook
|
|
|
|
runner_callbacks: more callbacks, this time for the runner API
|
2014-05-03 18:40:05 +02:00
|
|
|
stats: holds aggregrate data about events occurring to each host
|
2012-11-27 14:42:58 +01:00
|
|
|
inventory: can be specified instead of host_list to use a pre-existing inventory object
|
2013-02-04 01:46:25 +01:00
|
|
|
check: don't change anything, just try to detect some potential changes
|
2014-03-24 15:28:48 +01:00
|
|
|
any_errors_fatal: terminate the entire execution immediately when one of the hosts has failed
|
2014-07-03 03:02:28 +02:00
|
|
|
force_handlers: continue to notify and run handlers even if a task fails
|
2012-04-17 05:45:15 +02:00
|
|
|
"""
|
|
|
|
|
2012-07-26 02:45:51 +02:00
|
|
|
self.SETUP_CACHE = SETUP_CACHE
|
2014-04-01 16:48:14 +02:00
|
|
|
self.VARS_CACHE = VARS_CACHE
|
2012-06-01 04:08:00 +02:00
|
|
|
|
2014-01-17 10:30:57 +01:00
|
|
|
arguments = []
|
|
|
|
if playbook is None:
|
|
|
|
arguments.append('playbook')
|
|
|
|
if callbacks is None:
|
|
|
|
arguments.append('callbacks')
|
|
|
|
if runner_callbacks is None:
|
|
|
|
arguments.append('runner_callbacks')
|
|
|
|
if stats is None:
|
|
|
|
arguments.append('stats')
|
|
|
|
if arguments:
|
|
|
|
raise Exception('PlayBook missing required arguments: %s' % ', '.join(arguments))
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-04-27 01:56:10 +02:00
|
|
|
if extra_vars is None:
|
|
|
|
extra_vars = {}
|
2012-07-12 01:51:26 +02:00
|
|
|
if only_tags is None:
|
|
|
|
only_tags = [ 'all' ]
|
2013-06-25 07:33:29 +02:00
|
|
|
if skip_tags is None:
|
|
|
|
skip_tags = []
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-02-04 01:46:25 +01:00
|
|
|
self.check = check
|
2013-02-08 04:51:33 +01:00
|
|
|
self.diff = diff
|
2012-03-26 01:05:27 +02:00
|
|
|
self.module_path = module_path
|
|
|
|
self.forks = forks
|
|
|
|
self.timeout = timeout
|
|
|
|
self.remote_user = remote_user
|
|
|
|
self.remote_pass = remote_pass
|
2012-03-28 23:05:31 +02:00
|
|
|
self.remote_port = remote_port
|
2012-04-11 18:39:04 +02:00
|
|
|
self.transport = transport
|
2012-03-26 01:05:27 +02:00
|
|
|
self.callbacks = callbacks
|
|
|
|
self.runner_callbacks = runner_callbacks
|
|
|
|
self.stats = stats
|
2012-04-27 01:56:10 +02:00
|
|
|
self.extra_vars = extra_vars
|
2012-05-03 00:29:46 +02:00
|
|
|
self.global_vars = {}
|
2012-05-14 22:52:48 +02:00
|
|
|
self.private_key_file = private_key_file
|
2012-07-12 01:51:26 +02:00
|
|
|
self.only_tags = only_tags
|
2013-06-25 07:33:29 +02:00
|
|
|
self.skip_tags = skip_tags
|
2013-03-28 07:17:01 +01:00
|
|
|
self.any_errors_fatal = any_errors_fatal
|
2014-02-11 18:03:11 +01:00
|
|
|
self.vault_password = vault_password
|
2014-03-24 15:28:48 +01:00
|
|
|
self.force_handlers = force_handlers
|
2012-03-03 16:53:15 +01:00
|
|
|
|
2014-11-24 22:36:31 +01:00
|
|
|
self.become = become
|
|
|
|
self.become_method = become_method
|
|
|
|
self.become_user = become_user
|
|
|
|
self.become_pass = become_pass
|
|
|
|
|
2013-03-26 03:47:06 +01:00
|
|
|
self.callbacks.playbook = self
|
|
|
|
self.runner_callbacks.playbook = self
|
|
|
|
|
2012-11-27 14:42:58 +01:00
|
|
|
if inventory is None:
|
|
|
|
self.inventory = ansible.inventory.Inventory(host_list)
|
|
|
|
self.inventory.subset(subset)
|
|
|
|
else:
|
|
|
|
self.inventory = inventory
|
2012-10-13 02:07:05 +02:00
|
|
|
|
2013-07-28 18:47:26 +02:00
|
|
|
if self.module_path is not None:
|
2013-08-03 20:54:37 +02:00
|
|
|
utils.plugins.module_finder.add_directory(self.module_path)
|
2013-07-28 18:47:26 +02:00
|
|
|
|
2012-12-19 13:22:57 +01:00
|
|
|
self.basedir = os.path.dirname(playbook) or '.'
|
2013-06-05 23:25:24 +02:00
|
|
|
utils.plugins.push_basedir(self.basedir)
|
2014-03-19 11:09:38 +01:00
|
|
|
|
|
|
|
# let inventory know the playbook basedir so it can load more vars
|
|
|
|
self.inventory.set_playbook_basedir(self.basedir)
|
|
|
|
|
2013-07-18 22:45:18 +02:00
|
|
|
vars = extra_vars.copy()
|
2014-08-12 16:51:21 +02:00
|
|
|
vars['playbook_dir'] = os.path.abspath(self.basedir)
|
2013-03-26 03:32:01 +01:00
|
|
|
if self.inventory.basedir() is not None:
|
|
|
|
vars['inventory_dir'] = self.inventory.basedir()
|
2013-08-10 12:59:17 +02:00
|
|
|
|
|
|
|
if self.inventory.src() is not None:
|
|
|
|
vars['inventory_file'] = self.inventory.src()
|
|
|
|
|
2013-04-08 05:37:10 +02:00
|
|
|
self.filename = playbook
|
2013-03-26 03:32:01 +01:00
|
|
|
(self.playbook, self.play_basedirs) = self._load_playbook_from_file(playbook, vars)
|
2013-06-05 23:25:24 +02:00
|
|
|
ansible.callbacks.load_callback_plugins()
|
2014-01-08 21:40:37 +01:00
|
|
|
ansible.callbacks.set_playbook(self.callbacks, self)
|
2012-06-25 20:57:09 +02:00
|
|
|
|
2014-03-21 18:08:55 +01:00
|
|
|
self._ansible_version = utils.version_info(gitinfo=True)
|
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
2012-07-12 06:17:51 +02:00
|
|
|
|
2014-05-13 22:54:32 +02:00
|
|
|
def _get_playbook_vars(self, play_ds, existing_vars):
|
|
|
|
'''
|
2014-07-03 03:02:28 +02:00
|
|
|
Gets the vars specified with the play and blends them
|
2014-05-13 22:54:32 +02:00
|
|
|
with any existing vars that have already been read in
|
|
|
|
'''
|
|
|
|
new_vars = existing_vars.copy()
|
|
|
|
if 'vars' in play_ds:
|
|
|
|
if isinstance(play_ds['vars'], dict):
|
|
|
|
new_vars.update(play_ds['vars'])
|
|
|
|
elif isinstance(play_ds['vars'], list):
|
|
|
|
for v in play_ds['vars']:
|
|
|
|
new_vars.update(v)
|
|
|
|
return new_vars
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _get_include_info(self, play_ds, basedir, existing_vars={}):
|
|
|
|
'''
|
|
|
|
Gets any key=value pairs specified with the included file
|
|
|
|
name and returns the merged vars along with the path
|
|
|
|
'''
|
|
|
|
new_vars = existing_vars.copy()
|
2014-09-23 23:12:01 +02:00
|
|
|
tokens = split_args(play_ds.get('include', ''))
|
2014-05-13 22:54:32 +02:00
|
|
|
for t in tokens[1:]:
|
2014-09-23 23:12:01 +02:00
|
|
|
try:
|
|
|
|
(k,v) = unquote(t).split("=", 1)
|
|
|
|
new_vars[k] = template(basedir, v, new_vars)
|
|
|
|
except ValueError, e:
|
|
|
|
raise errors.AnsibleError('included playbook variables must be in the form k=v, got: %s' % t)
|
2014-05-13 22:54:32 +02:00
|
|
|
|
2014-09-23 23:12:01 +02:00
|
|
|
return (new_vars, unquote(tokens[0]))
|
2014-05-13 22:54:32 +02:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
|
|
|
def _get_playbook_vars_files(self, play_ds, existing_vars_files):
|
|
|
|
new_vars_files = list(existing_vars_files)
|
|
|
|
if 'vars_files' in play_ds:
|
|
|
|
new_vars_files = utils.list_union(new_vars_files, play_ds['vars_files'])
|
|
|
|
return new_vars_files
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2014-08-12 02:21:39 +02:00
|
|
|
def _extend_play_vars(self, play, vars={}):
|
|
|
|
'''
|
|
|
|
Extends the given play's variables with the additional specified vars.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if 'vars' not in play or not play['vars']:
|
|
|
|
# someone left out or put an empty "vars:" entry in their playbook
|
|
|
|
return vars.copy()
|
|
|
|
|
|
|
|
play_vars = None
|
2014-08-14 17:55:26 +02:00
|
|
|
if isinstance(play['vars'], dict):
|
2014-08-12 02:21:39 +02:00
|
|
|
play_vars = play['vars'].copy()
|
|
|
|
play_vars.update(vars)
|
2014-08-14 17:55:26 +02:00
|
|
|
elif isinstance(play['vars'], list):
|
2014-08-12 02:21:39 +02:00
|
|
|
# nobody should really do this, but handle vars: a=1 b=2
|
|
|
|
play_vars = play['vars'][:]
|
|
|
|
play_vars.extend([{k:v} for k,v in vars.iteritems()])
|
|
|
|
|
|
|
|
return play_vars
|
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2014-05-13 22:54:32 +02:00
|
|
|
def _load_playbook_from_file(self, path, vars={}, vars_files=[]):
|
2012-07-12 06:17:51 +02:00
|
|
|
'''
|
2012-07-15 17:20:59 +02:00
|
|
|
run top level error checking on playbooks and allow them to include other playbooks.
|
2012-07-12 06:17:51 +02:00
|
|
|
'''
|
|
|
|
|
2014-08-12 02:21:39 +02:00
|
|
|
playbook_data = utils.parse_yaml_from_file(path, vault_password=self.vault_password)
|
2012-07-12 06:17:51 +02:00
|
|
|
accumulated_plays = []
|
2012-09-10 04:43:27 +02:00
|
|
|
play_basedirs = []
|
2012-07-12 06:17:51 +02:00
|
|
|
|
|
|
|
if type(playbook_data) != list:
|
2014-01-03 19:46:31 +01:00
|
|
|
raise errors.AnsibleError("parse error: playbooks must be formatted as a YAML list, got %s" % type(playbook_data))
|
2012-07-12 06:17:51 +02:00
|
|
|
|
2012-12-19 13:22:57 +01:00
|
|
|
basedir = os.path.dirname(path) or '.'
|
2012-11-18 18:37:30 +01:00
|
|
|
utils.plugins.push_basedir(basedir)
|
2012-07-12 06:17:51 +02:00
|
|
|
for play in playbook_data:
|
2012-07-15 15:32:47 +02:00
|
|
|
if type(play) != dict:
|
2014-05-03 18:40:05 +02:00
|
|
|
raise errors.AnsibleError("parse error: each play in a playbook must be a YAML dictionary (hash), received: %s" % play)
|
2013-04-10 02:13:55 +02:00
|
|
|
|
2012-07-15 15:32:47 +02:00
|
|
|
if 'include' in play:
|
2013-04-10 02:13:55 +02:00
|
|
|
# a playbook (list of plays) decided to include some other list of plays
|
|
|
|
# from another file. The result is a flat list of plays in the end.
|
|
|
|
|
2014-05-13 22:54:32 +02:00
|
|
|
play_vars = self._get_playbook_vars(play, vars)
|
|
|
|
play_vars_files = self._get_playbook_vars_files(play, vars_files)
|
|
|
|
inc_vars, inc_path = self._get_include_info(play, basedir, play_vars)
|
|
|
|
play_vars.update(inc_vars)
|
2013-04-10 21:04:41 +02:00
|
|
|
|
2014-05-13 22:54:32 +02:00
|
|
|
included_path = utils.path_dwim(basedir, template(basedir, inc_path, play_vars))
|
|
|
|
(plays, basedirs) = self._load_playbook_from_file(included_path, vars=play_vars, vars_files=play_vars_files)
|
2013-04-10 21:04:41 +02:00
|
|
|
for p in plays:
|
|
|
|
# support for parameterized play includes works by passing
|
|
|
|
# those variables along to the subservient play
|
2014-08-12 02:21:39 +02:00
|
|
|
p['vars'] = self._extend_play_vars(p, play_vars)
|
2014-05-13 22:54:32 +02:00
|
|
|
# now add in the vars_files
|
|
|
|
p['vars_files'] = utils.list_union(p.get('vars_files', []), play_vars_files)
|
2013-04-10 21:04:41 +02:00
|
|
|
|
|
|
|
accumulated_plays.extend(plays)
|
|
|
|
play_basedirs.extend(basedirs)
|
|
|
|
|
2012-07-15 15:32:47 +02:00
|
|
|
else:
|
2013-04-10 02:13:55 +02:00
|
|
|
|
|
|
|
# this is a normal (non-included play)
|
2012-07-15 15:32:47 +02:00
|
|
|
accumulated_plays.append(play)
|
2012-11-09 13:09:20 +01:00
|
|
|
play_basedirs.append(basedir)
|
2012-07-12 06:17:51 +02:00
|
|
|
|
2012-09-10 04:43:27 +02:00
|
|
|
return (accumulated_plays, play_basedirs)
|
2012-07-12 06:17:51 +02:00
|
|
|
|
|
|
|
# *****************************************************
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-02-24 07:02:24 +01:00
|
|
|
def run(self):
|
2012-02-27 02:18:42 +01:00
|
|
|
''' run all patterns in the playbook '''
|
2012-08-26 05:10:43 +02:00
|
|
|
plays = []
|
|
|
|
matched_tags_all = set()
|
|
|
|
unmatched_tags_all = set()
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# loop through all patterns and run them
|
2012-03-07 01:24:36 +01:00
|
|
|
self.callbacks.on_start()
|
2012-09-10 04:43:27 +02:00
|
|
|
for (play_ds, play_basedir) in zip(self.playbook, self.play_basedirs):
|
2014-03-07 04:11:57 +01:00
|
|
|
play = Play(self, play_ds, play_basedir, vault_password=self.vault_password)
|
2013-04-20 15:09:35 +02:00
|
|
|
assert play is not None
|
2013-06-25 07:33:29 +02:00
|
|
|
|
2012-08-26 05:10:43 +02:00
|
|
|
matched_tags, unmatched_tags = play.compare_tags(self.only_tags)
|
2015-02-09 04:51:18 +01:00
|
|
|
|
2012-08-26 05:10:43 +02:00
|
|
|
matched_tags_all = matched_tags_all | matched_tags
|
|
|
|
unmatched_tags_all = unmatched_tags_all | unmatched_tags
|
|
|
|
|
2013-06-25 07:33:29 +02:00
|
|
|
# Remove tasks we wish to skip
|
|
|
|
matched_tags = matched_tags - set(self.skip_tags)
|
|
|
|
|
2012-08-26 05:10:43 +02:00
|
|
|
# if we have matched_tags, the play must be run.
|
|
|
|
# if the play contains no tasks, assume we just want to gather facts
|
2013-04-25 04:28:06 +02:00
|
|
|
# in this case there are actually 3 meta tasks (handler flushes) not 0
|
|
|
|
# tasks, so that's why there's a check against 3
|
|
|
|
if (len(matched_tags) > 0 or len(play.tasks()) == 3):
|
2012-08-26 05:10:43 +02:00
|
|
|
plays.append(play)
|
|
|
|
|
2013-06-25 07:33:29 +02:00
|
|
|
# if the playbook is invoked with --tags or --skip-tags that don't
|
|
|
|
# exist at all in the playbooks then we need to raise an error so that
|
|
|
|
# the user can correct the arguments.
|
|
|
|
unknown_tags = ((set(self.only_tags) | set(self.skip_tags)) -
|
|
|
|
(matched_tags_all | unmatched_tags_all))
|
2015-02-09 04:51:18 +01:00
|
|
|
|
|
|
|
for t in RESERVED_TAGS:
|
|
|
|
unknown_tags.discard(t)
|
2012-09-10 04:43:27 +02:00
|
|
|
|
2012-08-26 05:10:43 +02:00
|
|
|
if len(unknown_tags) > 0:
|
2015-02-09 04:51:18 +01:00
|
|
|
for t in RESERVED_TAGS:
|
|
|
|
unmatched_tags_all.discard(t)
|
2012-08-28 03:31:20 +02:00
|
|
|
msg = 'tag(s) not found in playbook: %s. possible values: %s'
|
|
|
|
unknown = ','.join(sorted(unknown_tags))
|
|
|
|
unmatched = ','.join(sorted(unmatched_tags_all))
|
|
|
|
raise errors.AnsibleError(msg % (unknown, unmatched))
|
2012-08-26 05:10:43 +02:00
|
|
|
|
|
|
|
for play in plays:
|
2013-06-09 18:17:57 +02:00
|
|
|
ansible.callbacks.set_play(self.callbacks, play)
|
|
|
|
ansible.callbacks.set_play(self.runner_callbacks, play)
|
2012-10-01 03:06:00 +02:00
|
|
|
if not self._run_play(play):
|
|
|
|
break
|
2014-05-16 01:17:36 +02:00
|
|
|
|
|
|
|
ansible.callbacks.set_play(self.callbacks, None)
|
|
|
|
ansible.callbacks.set_play(self.runner_callbacks, None)
|
2012-08-26 05:10:43 +02:00
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# summarize the results
|
2012-02-25 21:21:11 +01:00
|
|
|
results = {}
|
2012-03-26 01:05:27 +02:00
|
|
|
for host in self.stats.processed.keys():
|
|
|
|
results[host] = self.stats.summarize(host)
|
2012-02-25 21:21:11 +01:00
|
|
|
return results
|
2012-02-24 05:26:16 +01:00
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-04-26 20:35:19 +02:00
|
|
|
def _async_poll(self, poller, async_seconds, async_poll_interval):
|
2012-03-24 01:51:15 +01:00
|
|
|
''' launch an async job, if poll_interval is set, wait for completion '''
|
|
|
|
|
2012-04-26 20:35:19 +02:00
|
|
|
results = poller.wait(async_seconds, async_poll_interval)
|
2012-03-24 01:51:15 +01:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
# mark any hosts that are still listed as started as failed
|
|
|
|
# since these likely got killed by async_wrapper
|
2012-04-26 20:35:19 +02:00
|
|
|
for host in poller.hosts_to_poll:
|
|
|
|
reason = { 'failed' : 1, 'rc' : None, 'msg' : 'timed out' }
|
2014-04-01 16:48:14 +02:00
|
|
|
self.runner_callbacks.on_async_failed(host, reason, poller.runner.vars_cache[host]['ansible_job_id'])
|
2012-04-26 20:35:19 +02:00
|
|
|
results['contacted'][host] = reason
|
2012-03-24 01:51:15 +01:00
|
|
|
|
2012-03-13 01:53:10 +01:00
|
|
|
return results
|
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2015-04-04 22:37:14 +02:00
|
|
|
def _trim_unavailable_hosts(self, hostlist=[], keep_failed=False):
|
2012-11-28 01:03:18 +01:00
|
|
|
''' returns a list of hosts that haven't failed and aren't dark '''
|
|
|
|
|
2015-04-04 22:37:14 +02:00
|
|
|
return [ h for h in hostlist if (keep_failed or h not in self.stats.failures) and (h not in self.stats.dark)]
|
2012-11-28 01:03:18 +01:00
|
|
|
|
|
|
|
# *****************************************************
|
|
|
|
|
2015-04-04 22:37:14 +02:00
|
|
|
def _run_task_internal(self, task, include_failed=False):
|
2012-03-03 03:43:46 +01:00
|
|
|
''' run a particular module step in a playbook '''
|
2012-03-24 01:51:15 +01:00
|
|
|
|
2015-04-04 22:37:14 +02:00
|
|
|
hosts = self._trim_unavailable_hosts(self.inventory.list_hosts(task.play._play_hosts), keep_failed=include_failed)
|
2012-04-13 14:39:54 +02:00
|
|
|
self.inventory.restrict_to(hosts)
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-03-13 01:53:10 +01:00
|
|
|
runner = ansible.runner.Runner(
|
2014-01-21 02:19:03 +01:00
|
|
|
pattern=task.play.hosts,
|
|
|
|
inventory=self.inventory,
|
|
|
|
module_name=task.module_name,
|
|
|
|
module_args=task.module_args,
|
|
|
|
forks=self.forks,
|
|
|
|
remote_pass=self.remote_pass,
|
|
|
|
module_path=self.module_path,
|
|
|
|
timeout=self.timeout,
|
|
|
|
remote_user=task.remote_user,
|
|
|
|
remote_port=task.play.remote_port,
|
|
|
|
module_vars=task.module_vars,
|
2014-11-17 22:30:22 +01:00
|
|
|
play_vars=task.play_vars,
|
|
|
|
play_file_vars=task.play_file_vars,
|
|
|
|
role_vars=task.role_vars,
|
2014-11-20 19:20:32 +01:00
|
|
|
role_params=task.role_params,
|
2014-01-21 02:19:03 +01:00
|
|
|
default_vars=task.default_vars,
|
2014-08-15 22:57:02 +02:00
|
|
|
extra_vars=self.extra_vars,
|
2014-01-21 02:19:03 +01:00
|
|
|
private_key_file=self.private_key_file,
|
|
|
|
setup_cache=self.SETUP_CACHE,
|
2014-04-01 16:48:14 +02:00
|
|
|
vars_cache=self.VARS_CACHE,
|
2014-01-21 02:19:03 +01:00
|
|
|
basedir=task.play.basedir,
|
|
|
|
conditional=task.when,
|
|
|
|
callbacks=self.runner_callbacks,
|
|
|
|
transport=task.transport,
|
|
|
|
is_playbook=True,
|
|
|
|
check=self.check,
|
|
|
|
diff=self.diff,
|
|
|
|
environment=task.environment,
|
|
|
|
complex_args=task.args,
|
|
|
|
accelerate=task.play.accelerate,
|
|
|
|
accelerate_port=task.play.accelerate_port,
|
2013-11-04 23:20:03 +01:00
|
|
|
accelerate_ipv6=task.play.accelerate_ipv6,
|
2014-01-21 02:19:03 +01:00
|
|
|
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR,
|
2014-02-11 18:03:11 +01:00
|
|
|
vault_pass = self.vault_password,
|
2014-01-31 23:09:10 +01:00
|
|
|
run_hosts=hosts,
|
|
|
|
no_log=task.no_log,
|
2014-05-15 17:47:17 +02:00
|
|
|
run_once=task.run_once,
|
2014-11-24 22:36:31 +01:00
|
|
|
become=task.become,
|
|
|
|
become_method=task.become_method,
|
|
|
|
become_user=task.become_user,
|
|
|
|
become_pass=task.become_pass,
|
2012-03-13 01:53:10 +01:00
|
|
|
)
|
|
|
|
|
2014-01-26 00:43:15 +01:00
|
|
|
runner.module_vars.update({'play_hosts': hosts})
|
2014-03-21 18:08:55 +01:00
|
|
|
runner.module_vars.update({'ansible_version': self._ansible_version})
|
2014-01-26 00:43:15 +01:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
if task.async_seconds == 0:
|
2012-04-13 14:39:54 +02:00
|
|
|
results = runner.run()
|
2012-03-13 01:53:10 +01:00
|
|
|
else:
|
2012-05-26 06:37:34 +02:00
|
|
|
results, poller = runner.run_async(task.async_seconds)
|
2012-04-26 20:35:19 +02:00
|
|
|
self.stats.compute(results)
|
2012-05-26 06:37:34 +02:00
|
|
|
if task.async_poll_interval > 0:
|
|
|
|
# if not polling, playbook requested fire and forget, so don't poll
|
|
|
|
results = self._async_poll(poller, task.async_seconds, task.async_poll_interval)
|
2013-08-06 18:42:30 +02:00
|
|
|
else:
|
|
|
|
for (host, res) in results.get('contacted', {}).iteritems():
|
2014-04-01 16:48:14 +02:00
|
|
|
self.runner_callbacks.on_async_ok(host, res, poller.runner.vars_cache[host]['ansible_job_id'])
|
2012-04-13 14:39:54 +02:00
|
|
|
|
2012-10-01 02:48:35 +02:00
|
|
|
contacted = results.get('contacted',{})
|
|
|
|
dark = results.get('dark', {})
|
|
|
|
|
2012-04-13 14:39:54 +02:00
|
|
|
self.inventory.lift_restriction()
|
2012-10-01 02:48:35 +02:00
|
|
|
|
|
|
|
if len(contacted.keys()) == 0 and len(dark.keys()) == 0:
|
|
|
|
return None
|
|
|
|
|
2012-04-13 14:39:54 +02:00
|
|
|
return results
|
2012-03-14 02:30:34 +01:00
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
2012-03-03 03:43:46 +01:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
def _run_task(self, play, task, is_handler):
|
2012-03-24 01:51:15 +01:00
|
|
|
''' run a single task in the playbook and recursively run any subtasks. '''
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2013-04-20 15:09:35 +02:00
|
|
|
ansible.callbacks.set_task(self.callbacks, task)
|
|
|
|
ansible.callbacks.set_task(self.runner_callbacks, task)
|
2013-03-26 03:56:32 +01:00
|
|
|
|
2013-09-20 22:46:34 +02:00
|
|
|
if task.role_name:
|
2013-10-01 04:51:39 +02:00
|
|
|
name = '%s | %s' % (task.role_name, task.name)
|
2013-09-20 22:46:34 +02:00
|
|
|
else:
|
|
|
|
name = task.name
|
|
|
|
|
2015-02-26 23:05:33 +01:00
|
|
|
try:
|
|
|
|
# v1 HACK: we don't have enough information to template many names
|
|
|
|
# at this point. Rather than making this work for all cases in
|
|
|
|
# v1, just make this degrade gracefully. Will fix in v2
|
|
|
|
name = template(play.basedir, name, task.module_vars, lookup_fatal=False, filter_fatal=False)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.callbacks.on_task_start(name, is_handler)
|
2013-03-11 05:39:05 +01:00
|
|
|
if hasattr(self.callbacks, 'skip_task') and self.callbacks.skip_task:
|
2013-06-09 18:17:57 +02:00
|
|
|
ansible.callbacks.set_task(self.callbacks, None)
|
|
|
|
ansible.callbacks.set_task(self.runner_callbacks, None)
|
2013-03-11 05:39:05 +01:00
|
|
|
return True
|
2013-06-25 07:33:29 +02:00
|
|
|
|
2014-03-12 02:30:58 +01:00
|
|
|
# template ignore_errors
|
2015-02-26 23:05:33 +01:00
|
|
|
# TODO: Is this needed here? cond is templated again in
|
|
|
|
# check_conditional after some more manipulations.
|
|
|
|
# TODO: we don't have enough information here to template cond either
|
|
|
|
# (see note on templating name above)
|
2014-03-12 02:30:58 +01:00
|
|
|
cond = template(play.basedir, task.ignore_errors, task.module_vars, expand_lists=False)
|
2014-05-03 18:40:05 +02:00
|
|
|
task.ignore_errors = utils.check_conditional(cond, play.basedir, task.module_vars, fail_on_undefined=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR)
|
2014-03-12 02:30:58 +01:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
# load up an appropriate ansible runner to run the task in parallel
|
2015-04-04 22:37:14 +02:00
|
|
|
include_failed = is_handler and play.force_handlers
|
|
|
|
results = self._run_task_internal(task, include_failed=include_failed)
|
2012-10-01 02:48:35 +02:00
|
|
|
|
2012-07-15 17:20:59 +02:00
|
|
|
# if no hosts are matched, carry on
|
2012-10-01 02:48:35 +02:00
|
|
|
hosts_remaining = True
|
2012-07-15 17:20:59 +02:00
|
|
|
if results is None:
|
2012-10-01 02:48:35 +02:00
|
|
|
hosts_remaining = False
|
2012-07-15 17:20:59 +02:00
|
|
|
results = {}
|
2013-06-25 07:33:29 +02:00
|
|
|
|
2012-10-01 02:48:35 +02:00
|
|
|
contacted = results.get('contacted', {})
|
2012-09-04 22:40:07 +02:00
|
|
|
self.stats.compute(results, ignore_errors=task.ignore_errors)
|
2012-07-26 03:30:49 +02:00
|
|
|
|
2014-07-03 03:02:28 +02:00
|
|
|
def _register_play_vars(host, result):
|
|
|
|
# when 'register' is used, persist the result in the vars cache
|
2014-09-03 16:39:50 +02:00
|
|
|
# rather than the setup cache - vars should be transient between
|
|
|
|
# playbook executions
|
2014-07-03 03:02:28 +02:00
|
|
|
if 'stdout' in result and 'stdout_lines' not in result:
|
|
|
|
result['stdout_lines'] = result['stdout'].splitlines()
|
|
|
|
utils.update_hash(self.VARS_CACHE, host, {task.register: result})
|
|
|
|
|
2014-09-03 16:39:50 +02:00
|
|
|
def _save_play_facts(host, facts):
|
|
|
|
# saves play facts in SETUP_CACHE, unless the module executed was
|
|
|
|
# set_fact, in which case we add them to the VARS_CACHE
|
2014-11-17 22:30:22 +01:00
|
|
|
if task.module_name in ('set_fact', 'include_vars'):
|
2014-09-03 16:39:50 +02:00
|
|
|
utils.update_hash(self.VARS_CACHE, host, facts)
|
|
|
|
else:
|
|
|
|
utils.update_hash(self.SETUP_CACHE, host, facts)
|
|
|
|
|
2012-07-26 03:30:49 +02:00
|
|
|
# add facts to the global setup cache
|
2012-10-01 02:48:35 +02:00
|
|
|
for host, result in contacted.iteritems():
|
2013-08-09 04:08:46 +02:00
|
|
|
if 'results' in result:
|
|
|
|
# task ran with_ lookup plugin, so facts are encapsulated in
|
|
|
|
# multiple list items in the results key
|
|
|
|
for res in result['results']:
|
|
|
|
if type(res) == dict:
|
|
|
|
facts = res.get('ansible_facts', {})
|
2014-09-03 16:39:50 +02:00
|
|
|
_save_play_facts(host, facts)
|
2013-08-09 04:08:46 +02:00
|
|
|
else:
|
2014-07-03 03:02:28 +02:00
|
|
|
# when facts are returned, persist them in the setup cache
|
2013-08-09 04:08:46 +02:00
|
|
|
facts = result.get('ansible_facts', {})
|
2014-09-03 16:39:50 +02:00
|
|
|
_save_play_facts(host, facts)
|
|
|
|
|
|
|
|
# if requested, save the result into the registered variable name
|
2012-08-10 07:13:37 +02:00
|
|
|
if task.register:
|
2014-07-03 03:02:28 +02:00
|
|
|
_register_play_vars(host, result)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2013-04-18 04:27:00 +02:00
|
|
|
# also have to register some failed, but ignored, tasks
|
|
|
|
if task.ignore_errors and task.register:
|
|
|
|
failed = results.get('failed', {})
|
|
|
|
for host, result in failed.iteritems():
|
2014-07-03 03:02:28 +02:00
|
|
|
_register_play_vars(host, result)
|
2013-04-18 04:27:00 +02:00
|
|
|
|
2012-02-25 20:42:41 +01:00
|
|
|
# flag which notify handlers need to be run
|
2012-05-26 06:37:34 +02:00
|
|
|
if len(task.notify) > 0:
|
2012-03-24 01:51:15 +01:00
|
|
|
for host, results in results.get('contacted',{}).iteritems():
|
2012-02-25 23:16:23 +01:00
|
|
|
if results.get('changed', False):
|
2012-05-26 06:37:34 +02:00
|
|
|
for handler_name in task.notify:
|
2013-04-23 04:03:39 +02:00
|
|
|
self._flag_handler(play, template(play.basedir, handler_name, task.module_vars), host)
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2013-06-09 18:17:57 +02:00
|
|
|
ansible.callbacks.set_task(self.callbacks, None)
|
|
|
|
ansible.callbacks.set_task(self.runner_callbacks, None)
|
2012-10-01 02:48:35 +02:00
|
|
|
return hosts_remaining
|
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-11-23 00:31:38 +01:00
|
|
|
def _flag_handler(self, play, handler_name, host):
|
2012-08-07 02:07:02 +02:00
|
|
|
'''
|
2012-02-25 20:42:41 +01:00
|
|
|
if a task has any notify elements, flag handlers for run
|
|
|
|
at end of execution cycle for hosts that have indicated
|
|
|
|
changes have been made
|
|
|
|
'''
|
2012-02-27 02:18:42 +01:00
|
|
|
|
2012-03-27 03:31:48 +02:00
|
|
|
found = False
|
2012-11-23 00:31:38 +01:00
|
|
|
for x in play.handlers():
|
2013-04-23 04:03:39 +02:00
|
|
|
if handler_name == template(play.basedir, x.name, x.module_vars):
|
2012-03-27 03:31:48 +02:00
|
|
|
found = True
|
2012-05-26 06:37:34 +02:00
|
|
|
self.callbacks.on_notify(host, x.name)
|
|
|
|
x.notified_by.append(host)
|
2012-03-27 03:31:48 +02:00
|
|
|
if not found:
|
2012-05-26 06:37:34 +02:00
|
|
|
raise errors.AnsibleError("change handler (%s) is not defined" % handler_name)
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-07-14 18:24:19 +02:00
|
|
|
def _do_setup_step(self, play):
|
|
|
|
''' get facts from the remote system '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2014-03-03 16:20:42 +01:00
|
|
|
host_list = self._trim_unavailable_hosts(play._play_hosts)
|
2012-03-20 03:42:31 +01:00
|
|
|
|
2014-03-15 21:19:28 +01:00
|
|
|
if play.gather_facts is None and C.DEFAULT_GATHERING == 'smart':
|
|
|
|
host_list = [h for h in host_list if h not in self.SETUP_CACHE or 'module_setup' not in self.SETUP_CACHE[h]]
|
|
|
|
if len(host_list) == 0:
|
|
|
|
return {}
|
2014-03-15 23:10:39 +01:00
|
|
|
elif play.gather_facts is False or (play.gather_facts is None and C.DEFAULT_GATHERING == 'explicit'):
|
2014-03-15 21:19:28 +01:00
|
|
|
return {}
|
|
|
|
|
2012-07-14 18:24:19 +02:00
|
|
|
self.callbacks.on_setup()
|
2012-04-13 14:39:54 +02:00
|
|
|
self.inventory.restrict_to(host_list)
|
2013-06-25 07:33:29 +02:00
|
|
|
|
2013-04-20 15:09:35 +02:00
|
|
|
ansible.callbacks.set_task(self.callbacks, None)
|
|
|
|
ansible.callbacks.set_task(self.runner_callbacks, None)
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-03-06 02:09:03 +01:00
|
|
|
# push any variables down to the system
|
|
|
|
setup_results = ansible.runner.Runner(
|
2014-05-14 15:22:02 +02:00
|
|
|
basedir=self.basedir,
|
2014-01-21 02:19:03 +01:00
|
|
|
pattern=play.hosts,
|
|
|
|
module_name='setup',
|
|
|
|
module_args={},
|
|
|
|
inventory=self.inventory,
|
|
|
|
forks=self.forks,
|
|
|
|
module_path=self.module_path,
|
|
|
|
timeout=self.timeout,
|
|
|
|
remote_user=play.remote_user,
|
|
|
|
remote_pass=self.remote_pass,
|
|
|
|
remote_port=play.remote_port,
|
|
|
|
private_key_file=self.private_key_file,
|
|
|
|
setup_cache=self.SETUP_CACHE,
|
2014-04-01 16:48:14 +02:00
|
|
|
vars_cache=self.VARS_CACHE,
|
2014-01-21 02:19:03 +01:00
|
|
|
callbacks=self.runner_callbacks,
|
2014-11-24 22:36:31 +01:00
|
|
|
become=play.become,
|
|
|
|
become_method=play.become_method,
|
|
|
|
become_user=play.become_user,
|
|
|
|
become_pass=self.become_pass,
|
2014-02-11 18:03:11 +01:00
|
|
|
vault_pass=self.vault_password,
|
2014-01-27 23:26:31 +01:00
|
|
|
transport=play.transport,
|
2014-01-21 02:19:03 +01:00
|
|
|
is_playbook=True,
|
|
|
|
module_vars=play.vars,
|
2014-11-17 22:30:22 +01:00
|
|
|
play_vars=play.vars,
|
|
|
|
play_file_vars=play.vars_file_vars,
|
|
|
|
role_vars=play.role_vars,
|
2014-01-21 02:19:03 +01:00
|
|
|
default_vars=play.default_vars,
|
|
|
|
check=self.check,
|
|
|
|
diff=self.diff,
|
|
|
|
accelerate=play.accelerate,
|
|
|
|
accelerate_port=play.accelerate_port,
|
2012-03-06 02:09:03 +01:00
|
|
|
).run()
|
2012-03-26 01:05:27 +02:00
|
|
|
self.stats.compute(setup_results, setup=True)
|
2012-03-14 02:30:34 +01:00
|
|
|
|
2012-04-13 14:39:54 +02:00
|
|
|
self.inventory.lift_restriction()
|
|
|
|
|
2012-03-06 02:09:03 +01:00
|
|
|
# now for each result, load into the setup cache so we can
|
|
|
|
# let runner template out future commands
|
|
|
|
setup_ok = setup_results.get('contacted', {})
|
2012-07-14 18:24:19 +02:00
|
|
|
for (host, result) in setup_ok.iteritems():
|
2014-07-03 03:02:28 +02:00
|
|
|
utils.update_hash(self.SETUP_CACHE, host, {'module_setup': True})
|
|
|
|
utils.update_hash(self.SETUP_CACHE, host, result.get('ansible_facts', {}))
|
2012-05-26 06:37:34 +02:00
|
|
|
return setup_results
|
2012-03-20 03:42:31 +01:00
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2013-04-08 05:37:10 +02:00
|
|
|
|
|
|
|
def generate_retry_inventory(self, replay_hosts):
|
|
|
|
'''
|
2014-05-03 18:40:05 +02:00
|
|
|
called by /usr/bin/ansible when a playbook run fails. It generates an inventory
|
2013-04-08 05:37:10 +02:00
|
|
|
that allows re-running on ONLY the failed hosts. This may duplicate some
|
|
|
|
variable information in group_vars/host_vars but that is ok, and expected.
|
2013-06-25 07:33:29 +02:00
|
|
|
'''
|
2013-04-08 05:37:10 +02:00
|
|
|
|
|
|
|
buf = StringIO.StringIO()
|
2013-04-08 18:36:01 +02:00
|
|
|
for x in replay_hosts:
|
|
|
|
buf.write("%s\n" % x)
|
2014-01-06 22:36:42 +01:00
|
|
|
basedir = C.shell_expand_path(C.RETRY_FILES_SAVE_PATH)
|
2013-04-08 18:36:01 +02:00
|
|
|
filename = "%s.retry" % os.path.basename(self.filename)
|
2013-04-08 18:38:05 +02:00
|
|
|
filename = filename.replace(".yml","")
|
2014-01-06 22:36:42 +01:00
|
|
|
filename = os.path.join(basedir, filename)
|
2013-04-08 05:37:10 +02:00
|
|
|
|
|
|
|
try:
|
2014-01-06 22:36:42 +01:00
|
|
|
if not os.path.exists(basedir):
|
|
|
|
os.makedirs(basedir)
|
|
|
|
|
2013-04-08 05:37:10 +02:00
|
|
|
fd = open(filename, 'w')
|
|
|
|
fd.write(buf.getvalue())
|
|
|
|
fd.close()
|
2013-04-08 18:36:01 +02:00
|
|
|
except:
|
2014-01-06 22:36:42 +01:00
|
|
|
ansible.callbacks.display(
|
|
|
|
"\nERROR: could not create retry file. Check the value of \n"
|
|
|
|
+ "the configuration variable 'retry_files_save_path' or set \n"
|
|
|
|
+ "'retry_files_enabled' to False to avoid this message.\n",
|
|
|
|
color='red'
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return filename
|
2013-04-08 05:37:10 +02:00
|
|
|
|
|
|
|
# *****************************************************
|
2015-02-13 02:58:30 +01:00
|
|
|
def tasks_to_run_in_play(self, play):
|
2013-04-08 05:37:10 +02:00
|
|
|
|
2015-02-13 02:58:30 +01:00
|
|
|
tasks = []
|
|
|
|
|
|
|
|
for task in play.tasks():
|
|
|
|
# only run the task if the requested tags match or has 'always' tag
|
|
|
|
u = set(['untagged'])
|
|
|
|
task_set = set(task.tags)
|
|
|
|
|
|
|
|
if 'always' in task.tags:
|
|
|
|
should_run = True
|
|
|
|
else:
|
|
|
|
if 'all' in self.only_tags:
|
|
|
|
should_run = True
|
|
|
|
else:
|
|
|
|
should_run = False
|
|
|
|
if 'tagged' in self.only_tags:
|
|
|
|
if task_set != u:
|
|
|
|
should_run = True
|
|
|
|
elif 'untagged' in self.only_tags:
|
|
|
|
if task_set == u:
|
|
|
|
should_run = True
|
|
|
|
else:
|
|
|
|
if task_set.intersection(self.only_tags):
|
|
|
|
should_run = True
|
|
|
|
|
|
|
|
# Check for tags that we need to skip
|
|
|
|
if 'all' in self.skip_tags:
|
|
|
|
should_run = False
|
|
|
|
else:
|
|
|
|
if 'tagged' in self.skip_tags:
|
|
|
|
if task_set != u:
|
|
|
|
should_run = False
|
|
|
|
elif 'untagged' in self.skip_tags:
|
|
|
|
if task_set == u:
|
|
|
|
should_run = False
|
|
|
|
else:
|
|
|
|
if should_run:
|
|
|
|
if task_set.intersection(self.skip_tags):
|
|
|
|
should_run = False
|
|
|
|
|
|
|
|
if should_run:
|
|
|
|
tasks.append(task)
|
|
|
|
|
|
|
|
return tasks
|
|
|
|
|
|
|
|
# *****************************************************
|
2012-05-26 06:37:34 +02:00
|
|
|
def _run_play(self, play):
|
2012-03-24 01:51:15 +01:00
|
|
|
''' run a list of tasks for a given pattern, in order '''
|
2014-07-03 03:02:28 +02:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
self.callbacks.on_play_start(play.name)
|
2014-01-07 03:16:43 +01:00
|
|
|
# Get the hosts for this play
|
|
|
|
play._play_hosts = self.inventory.list_hosts(play.hosts)
|
2012-10-02 15:15:55 +02:00
|
|
|
# if no hosts matches this play, drop out
|
2014-01-07 03:16:43 +01:00
|
|
|
if not play._play_hosts:
|
2012-10-02 15:15:55 +02:00
|
|
|
self.callbacks.on_no_hosts_matched()
|
|
|
|
return True
|
|
|
|
|
2012-07-14 18:24:19 +02:00
|
|
|
# get facts from system
|
2012-08-14 05:36:51 +02:00
|
|
|
self._do_setup_step(play)
|
2012-05-26 06:37:34 +02:00
|
|
|
|
2012-03-20 03:42:31 +01:00
|
|
|
# now with that data, handle contentional variable file imports!
|
2014-01-07 03:16:43 +01:00
|
|
|
all_hosts = self._trim_unavailable_hosts(play._play_hosts)
|
2014-02-11 18:03:11 +01:00
|
|
|
play.update_vars_files(all_hosts, vault_password=self.vault_password)
|
2014-01-07 03:16:43 +01:00
|
|
|
hosts_count = len(all_hosts)
|
2012-08-18 15:52:13 +02:00
|
|
|
|
2013-11-27 11:59:12 +01:00
|
|
|
if play.serial.endswith("%"):
|
|
|
|
|
|
|
|
# This is a percentage, so calculate it based on the
|
|
|
|
# number of hosts
|
|
|
|
serial_pct = int(play.serial.replace("%",""))
|
2014-02-25 13:11:05 +01:00
|
|
|
serial = int((serial_pct/100.0) * len(all_hosts))
|
2013-11-27 11:59:12 +01:00
|
|
|
|
|
|
|
# Ensure that no matter how small the percentage, serial
|
|
|
|
# can never fall below 1, so that things actually happen
|
|
|
|
serial = max(serial, 1)
|
|
|
|
else:
|
|
|
|
serial = int(play.serial)
|
|
|
|
|
2012-08-18 15:52:13 +02:00
|
|
|
serialized_batch = []
|
2013-11-27 11:59:12 +01:00
|
|
|
if serial <= 0:
|
2012-08-18 15:52:13 +02:00
|
|
|
serialized_batch = [all_hosts]
|
|
|
|
else:
|
|
|
|
# do N forks all the way through before moving to next
|
|
|
|
while len(all_hosts) > 0:
|
|
|
|
play_hosts = []
|
2013-11-27 11:59:12 +01:00
|
|
|
for x in range(serial):
|
2012-08-18 15:52:13 +02:00
|
|
|
if len(all_hosts) > 0:
|
2014-08-19 16:02:50 +02:00
|
|
|
play_hosts.append(all_hosts.pop(0))
|
2012-08-26 05:10:43 +02:00
|
|
|
serialized_batch.append(play_hosts)
|
2012-08-18 15:52:13 +02:00
|
|
|
|
2014-03-25 05:20:11 +01:00
|
|
|
task_errors = False
|
2012-08-18 15:52:13 +02:00
|
|
|
for on_hosts in serialized_batch:
|
|
|
|
|
2014-01-07 03:16:43 +01:00
|
|
|
# restrict the play to just the hosts we have in our on_hosts block that are
|
|
|
|
# available.
|
|
|
|
play._play_hosts = self._trim_unavailable_hosts(on_hosts)
|
2012-08-18 15:52:13 +02:00
|
|
|
self.inventory.also_restrict_to(on_hosts)
|
|
|
|
|
2015-02-13 02:58:30 +01:00
|
|
|
for task in self.tasks_to_run_in_play(play):
|
2013-04-21 00:03:03 +02:00
|
|
|
|
|
|
|
if task.meta is not None:
|
2014-03-25 18:32:11 +01:00
|
|
|
# meta tasks can force handlers to run mid-play
|
|
|
|
if task.meta == 'flush_handlers':
|
|
|
|
self.run_handlers(play)
|
|
|
|
|
|
|
|
# skip calling the handler till the play is finished
|
2014-03-24 15:28:48 +01:00
|
|
|
continue
|
2013-04-21 00:03:03 +02:00
|
|
|
|
2015-02-13 02:58:30 +01:00
|
|
|
if not self._run_task(play, task, False):
|
|
|
|
# whether no hosts matched is fatal or not depends if it was on the initial step.
|
|
|
|
# if we got exactly no hosts on the first step (setup!) then the host group
|
|
|
|
# just didn't match anything and that's ok
|
|
|
|
return False
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2014-01-07 03:16:43 +01:00
|
|
|
# Get a new list of what hosts are left as available, the ones that
|
|
|
|
# did not go fail/dark during the task
|
|
|
|
host_list = self._trim_unavailable_hosts(play._play_hosts)
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2013-09-07 07:37:17 +02:00
|
|
|
# Set max_fail_pct to 0, So if any hosts fails, bail out
|
|
|
|
if task.any_errors_fatal and len(host_list) < hosts_count:
|
|
|
|
play.max_fail_pct = 0
|
|
|
|
|
2014-05-03 18:40:05 +02:00
|
|
|
# If threshold for max nodes failed is exceeded, bail out.
|
2014-03-21 18:31:47 +01:00
|
|
|
if play.serial > 0:
|
|
|
|
# if serial is set, we need to shorten the size of host_count
|
|
|
|
play_count = len(play._play_hosts)
|
|
|
|
if (play_count - len(host_list)) > int((play.max_fail_pct)/100.0 * play_count):
|
|
|
|
host_list = None
|
|
|
|
else:
|
|
|
|
if (hosts_count - len(host_list)) > int((play.max_fail_pct)/100.0 * hosts_count):
|
|
|
|
host_list = None
|
2013-11-07 19:54:06 +01:00
|
|
|
|
2012-10-02 15:15:55 +02:00
|
|
|
# if no hosts remain, drop out
|
|
|
|
if not host_list:
|
2015-04-04 22:37:14 +02:00
|
|
|
if play.force_handlers:
|
2014-03-25 15:35:05 +01:00
|
|
|
task_errors = True
|
2014-03-25 05:20:11 +01:00
|
|
|
break
|
2014-03-24 15:28:48 +01:00
|
|
|
else:
|
2014-03-25 18:32:11 +01:00
|
|
|
self.callbacks.on_no_hosts_remaining()
|
2014-03-24 15:28:48 +01:00
|
|
|
return False
|
2012-10-02 15:15:55 +02:00
|
|
|
|
2014-03-25 18:32:11 +01:00
|
|
|
# lift restrictions after each play finishes
|
2014-03-25 05:20:11 +01:00
|
|
|
self.inventory.lift_also_restriction()
|
2014-03-25 18:32:11 +01:00
|
|
|
|
2015-04-04 22:37:14 +02:00
|
|
|
if task_errors and not play.force_handlers:
|
2014-03-25 18:32:11 +01:00
|
|
|
# if there were failed tasks and handler execution
|
|
|
|
# is not forced, quit the play with an error
|
2014-03-25 05:20:11 +01:00
|
|
|
return False
|
2014-03-25 18:32:11 +01:00
|
|
|
else:
|
|
|
|
# no errors, go ahead and execute all handlers
|
|
|
|
if not self.run_handlers(play):
|
|
|
|
return False
|
2014-03-25 05:20:11 +01:00
|
|
|
|
2012-10-01 03:06:00 +02:00
|
|
|
return True
|
2012-08-18 15:52:13 +02:00
|
|
|
|
2014-03-24 15:28:48 +01:00
|
|
|
|
|
|
|
def run_handlers(self, play):
|
|
|
|
on_hosts = play._play_hosts
|
|
|
|
hosts_count = len(on_hosts)
|
|
|
|
for task in play.tasks():
|
|
|
|
if task.meta is not None:
|
|
|
|
|
2014-03-25 18:32:11 +01:00
|
|
|
fired_names = {}
|
|
|
|
for handler in play.handlers():
|
|
|
|
if len(handler.notified_by) > 0:
|
|
|
|
self.inventory.restrict_to(handler.notified_by)
|
|
|
|
|
|
|
|
# Resolve the variables first
|
|
|
|
handler_name = template(play.basedir, handler.name, handler.module_vars)
|
|
|
|
if handler_name not in fired_names:
|
|
|
|
self._run_task(play, handler, True)
|
|
|
|
# prevent duplicate handler includes from running more than once
|
|
|
|
fired_names[handler_name] = 1
|
|
|
|
|
|
|
|
host_list = self._trim_unavailable_hosts(play._play_hosts)
|
|
|
|
if handler.any_errors_fatal and len(host_list) < hosts_count:
|
|
|
|
play.max_fail_pct = 0
|
|
|
|
if (hosts_count - len(host_list)) > int((play.max_fail_pct)/100.0 * hosts_count):
|
|
|
|
host_list = None
|
2015-04-04 22:37:14 +02:00
|
|
|
if not host_list and not play.force_handlers:
|
2014-03-25 18:32:11 +01:00
|
|
|
self.callbacks.on_no_hosts_remaining()
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.inventory.lift_restriction()
|
|
|
|
new_list = handler.notified_by[:]
|
|
|
|
for host in handler.notified_by:
|
|
|
|
if host in on_hosts:
|
|
|
|
while host in new_list:
|
|
|
|
new_list.remove(host)
|
|
|
|
handler.notified_by = new_list
|
|
|
|
|
|
|
|
continue
|
2014-03-24 15:28:48 +01:00
|
|
|
|
|
|
|
return True
|