2014-10-02 17:07:05 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
2014-10-15 22:53:43 +00:00
|
|
|
|
2014-10-15 23:18:12 +00:00
|
|
|
# Make coding more python3-ish
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
2014-10-15 23:18:12 +00:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-07-21 18:51:53 +00:00
|
|
|
import getpass
|
2015-07-21 22:09:31 +00:00
|
|
|
import locale
|
2015-10-15 07:41:00 +00:00
|
|
|
import os
|
2014-11-14 22:14:08 +00:00
|
|
|
import signal
|
2015-07-21 18:51:53 +00:00
|
|
|
import sys
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-10-16 00:55:23 +00:00
|
|
|
from ansible.compat.six import string_types
|
2015-10-13 06:56:12 +00:00
|
|
|
|
2016-01-26 19:11:28 +00:00
|
|
|
from ansible import constants as C
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
|
|
|
from ansible.playbook import Playbook
|
2015-05-02 04:48:11 +00:00
|
|
|
from ansible.template import Templar
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-07-30 17:57:39 +00:00
|
|
|
from ansible.utils.unicode import to_unicode
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-11-10 19:40:55 +00:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
2014-10-15 23:37:29 +00:00
|
|
|
class PlaybookExecutor:
|
2014-10-15 22:53:43 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
'''
|
|
|
|
This is the primary class for executing playbooks, and thus the
|
|
|
|
basis for bin/ansible-playbook operation.
|
|
|
|
'''
|
|
|
|
|
2015-11-10 19:40:55 +00:00
|
|
|
def __init__(self, playbooks, inventory, variable_manager, loader, options, passwords):
|
2014-11-14 22:14:08 +00:00
|
|
|
self._playbooks = playbooks
|
|
|
|
self._inventory = inventory
|
|
|
|
self._variable_manager = variable_manager
|
|
|
|
self._loader = loader
|
|
|
|
self._options = options
|
2015-04-08 07:16:13 +00:00
|
|
|
self.passwords = passwords
|
2015-09-09 19:26:40 +00:00
|
|
|
self._unreachable_hosts = dict()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-05-08 23:19:03 +00:00
|
|
|
if options.listhosts or options.listtasks or options.listtags or options.syntax:
|
2015-04-03 17:02:42 +00:00
|
|
|
self._tqm = None
|
|
|
|
else:
|
2015-11-10 19:40:55 +00:00
|
|
|
self._tqm = TaskQueueManager(inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=self.passwords)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
'''
|
|
|
|
Run the given playbook, based on the settings in the play which
|
|
|
|
may limit the runs to serialized groups, etc.
|
|
|
|
'''
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, self._cleanup)
|
|
|
|
|
2015-01-12 22:04:56 +00:00
|
|
|
result = 0
|
2015-04-04 14:25:55 +00:00
|
|
|
entrylist = []
|
|
|
|
entry = {}
|
2014-11-14 22:14:08 +00:00
|
|
|
try:
|
|
|
|
for playbook_path in self._playbooks:
|
|
|
|
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
|
2015-07-14 13:26:24 +00:00
|
|
|
self._inventory.set_playbook_basedir(os.path.dirname(playbook_path))
|
2015-04-03 17:02:42 +00:00
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
if self._tqm is None: # we are doing a listing
|
|
|
|
entry = {'playbook': playbook_path}
|
|
|
|
entry['plays'] = []
|
2015-12-02 17:29:51 +00:00
|
|
|
else:
|
|
|
|
# make sure the tqm has callbacks loaded
|
|
|
|
self._tqm.load_callbacks()
|
|
|
|
self._tqm.send_callback('v2_playbook_on_start', pb)
|
2015-04-04 14:25:55 +00:00
|
|
|
|
|
|
|
i = 1
|
2015-04-04 20:26:05 +00:00
|
|
|
plays = pb.get_plays()
|
2015-11-10 19:40:55 +00:00
|
|
|
display.vv('%d plays in %s' % (len(plays), playbook_path))
|
2015-04-04 20:26:05 +00:00
|
|
|
|
|
|
|
for play in plays:
|
2015-09-29 04:25:59 +00:00
|
|
|
if play._included_path is not None:
|
|
|
|
self._loader.set_basedir(play._included_path)
|
2015-10-17 16:44:28 +00:00
|
|
|
else:
|
|
|
|
self._loader.set_basedir(pb._basedir)
|
2015-09-29 04:25:59 +00:00
|
|
|
|
2015-07-08 01:46:44 +00:00
|
|
|
# clear any filters which may have been applied to the inventory
|
2014-11-14 22:14:08 +00:00
|
|
|
self._inventory.remove_restriction()
|
|
|
|
|
2015-07-21 18:51:53 +00:00
|
|
|
if play.vars_prompt:
|
|
|
|
for var in play.vars_prompt:
|
|
|
|
vname = var['name']
|
|
|
|
prompt = var.get("prompt", vname)
|
|
|
|
default = var.get("default", None)
|
|
|
|
private = var.get("private", True)
|
|
|
|
confirm = var.get("confirm", False)
|
|
|
|
encrypt = var.get("encrypt", None)
|
|
|
|
salt_size = var.get("salt_size", None)
|
|
|
|
salt = var.get("salt", None)
|
|
|
|
|
2016-01-08 16:43:27 +00:00
|
|
|
if vname not in self._variable_manager.extra_vars:
|
2015-10-08 14:14:33 +00:00
|
|
|
if self._tqm:
|
2016-01-13 15:17:43 +00:00
|
|
|
self._tqm.send_callback('v2_playbook_on_vars_prompt', vname, private, prompt, encrypt, confirm, salt_size, salt, default)
|
2016-01-08 16:43:27 +00:00
|
|
|
play.vars[vname] = display.do_var_prompt(vname, private, prompt, encrypt, confirm, salt_size, salt, default)
|
|
|
|
else: # we are either in --list-<option> or syntax check
|
2015-11-27 19:41:00 +00:00
|
|
|
play.vars[vname] = default
|
2015-07-21 18:51:53 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
# Create a temporary copy of the play here, so we can run post_validate
|
|
|
|
# on it without the templating changes affecting the original object.
|
|
|
|
all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
|
2015-06-11 03:26:01 +00:00
|
|
|
templar = Templar(loader=self._loader, variables=all_vars)
|
2014-11-14 22:14:08 +00:00
|
|
|
new_play = play.copy()
|
2015-05-02 04:48:11 +00:00
|
|
|
new_play.post_validate(templar)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-05-08 23:19:03 +00:00
|
|
|
if self._options.syntax:
|
|
|
|
continue
|
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
if self._tqm is None:
|
|
|
|
# we are just doing a listing
|
2015-07-29 02:03:50 +00:00
|
|
|
entry['plays'].append(new_play)
|
2015-04-04 14:25:55 +00:00
|
|
|
|
|
|
|
else:
|
2015-09-09 19:26:40 +00:00
|
|
|
self._tqm._unreachable_hosts.update(self._unreachable_hosts)
|
2015-07-04 03:52:49 +00:00
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
# we are actually running plays
|
|
|
|
for batch in self._get_serialized_batches(new_play):
|
|
|
|
if len(batch) == 0:
|
|
|
|
self._tqm.send_callback('v2_playbook_on_play_start', new_play)
|
|
|
|
self._tqm.send_callback('v2_playbook_on_no_hosts_matched')
|
|
|
|
break
|
2015-08-26 16:03:13 +00:00
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
# restrict the inventory to the hosts in the serialized batch
|
|
|
|
self._inventory.restrict_to_hosts(batch)
|
|
|
|
# and run it...
|
|
|
|
result = self._tqm.run(play=play)
|
2015-08-26 16:03:13 +00:00
|
|
|
|
|
|
|
# check the number of failures here, to see if they're above the maximum
|
|
|
|
# failure percentage allowed, or if any errors are fatal. If either of those
|
|
|
|
# conditions are met, we break out, otherwise we only break out if the entire
|
|
|
|
# batch failed
|
|
|
|
failed_hosts_count = len(self._tqm._failed_hosts) + len(self._tqm._unreachable_hosts)
|
2016-01-15 18:14:27 +00:00
|
|
|
if new_play.max_fail_percentage is not None and \
|
2015-08-26 16:03:13 +00:00
|
|
|
int((new_play.max_fail_percentage)/100.0 * len(batch)) > int((len(batch) - failed_hosts_count) / len(batch) * 100.0):
|
2015-04-04 14:25:55 +00:00
|
|
|
break
|
2015-08-26 16:03:13 +00:00
|
|
|
elif len(batch) == failed_hosts_count:
|
|
|
|
break
|
|
|
|
|
|
|
|
# clear the failed hosts dictionaires in the TQM for the next batch
|
2015-09-09 19:26:40 +00:00
|
|
|
self._unreachable_hosts.update(self._tqm._unreachable_hosts)
|
2015-08-26 16:03:13 +00:00
|
|
|
self._tqm.clear_failed_hosts()
|
2015-04-04 14:25:55 +00:00
|
|
|
|
2015-09-09 19:26:40 +00:00
|
|
|
# if the last result wasn't zero or 3 (some hosts were unreachable),
|
|
|
|
# break out of the serial batch loop
|
|
|
|
if result not in (0, 3):
|
2015-04-05 06:05:17 +00:00
|
|
|
break
|
2015-04-04 14:25:55 +00:00
|
|
|
|
|
|
|
i = i + 1 # per play
|
|
|
|
|
|
|
|
if entry:
|
|
|
|
entrylist.append(entry) # per playbook
|
|
|
|
|
2015-11-28 19:02:50 +00:00
|
|
|
# send the stats callback for this playbook
|
|
|
|
if self._tqm is not None:
|
2016-01-27 14:56:19 +00:00
|
|
|
if C.RETRY_FILES_ENABLED:
|
|
|
|
retries = list(set(self._tqm._failed_hosts.keys() + self._tqm._unreachable_hosts.keys()))
|
|
|
|
retries.sort()
|
|
|
|
if len(retries) > 0:
|
|
|
|
if C.RETRY_FILES_SAVE_PATH:
|
|
|
|
basedir = C.shell_expand(C.RETRY_FILES_SAVE_PATH)
|
|
|
|
else:
|
|
|
|
basedir = os.path.dirname(playbook_path)
|
|
|
|
|
|
|
|
(retry_name, _) = os.path.splitext(os.path.basename(playbook_path))
|
|
|
|
filename = os.path.join(basedir, "%s.retry" % retry_name)
|
|
|
|
if self._generate_retry_inventory(filename, retries):
|
|
|
|
display.display("\tto retry, use: --limit @%s\n" % filename)
|
|
|
|
|
2015-11-28 19:02:50 +00:00
|
|
|
self._tqm.send_callback('v2_playbook_on_stats', self._tqm._stats)
|
|
|
|
|
2015-05-05 18:41:32 +00:00
|
|
|
# if the last result wasn't zero, break out of the playbook file name loop
|
|
|
|
if result != 0:
|
2015-09-09 22:10:19 +00:00
|
|
|
break
|
2015-05-05 18:41:32 +00:00
|
|
|
|
2015-04-04 14:25:55 +00:00
|
|
|
if entrylist:
|
|
|
|
return entrylist
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-04-03 17:02:42 +00:00
|
|
|
finally:
|
2015-04-04 14:25:55 +00:00
|
|
|
if self._tqm is not None:
|
|
|
|
self._cleanup()
|
|
|
|
|
2015-05-08 23:19:03 +00:00
|
|
|
if self._options.syntax:
|
2015-11-10 19:40:55 +00:00
|
|
|
display.display("No issues encountered")
|
2015-05-08 23:19:03 +00:00
|
|
|
return result
|
|
|
|
|
2015-01-12 22:04:56 +00:00
|
|
|
return result
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def _cleanup(self, signum=None, framenum=None):
|
2015-01-12 22:04:56 +00:00
|
|
|
return self._tqm.cleanup()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def _get_serialized_batches(self, play):
|
|
|
|
'''
|
|
|
|
Returns a list of hosts, subdivided into batches based on
|
|
|
|
the serial size specified in the play.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# make sure we have a unique list of hosts
|
|
|
|
all_hosts = self._inventory.get_hosts(play.hosts)
|
|
|
|
|
|
|
|
# check to see if the serial number was specified as a percentage,
|
|
|
|
# and convert it to an integer value based on the number of hosts
|
2015-10-13 06:56:12 +00:00
|
|
|
if isinstance(play.serial, string_types) and play.serial.endswith('%'):
|
2014-11-14 22:14:08 +00:00
|
|
|
serial_pct = int(play.serial.replace("%",""))
|
|
|
|
serial = int((serial_pct/100.0) * len(all_hosts))
|
|
|
|
else:
|
2015-11-10 19:40:55 +00:00
|
|
|
if play.serial is None:
|
2015-10-12 13:00:43 +00:00
|
|
|
serial = -1
|
|
|
|
else:
|
|
|
|
serial = int(play.serial)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# if the serial count was not specified or is invalid, default to
|
|
|
|
# a list of all hosts, otherwise split the list of hosts into chunks
|
|
|
|
# which are based on the serial size
|
|
|
|
if serial <= 0:
|
|
|
|
return [all_hosts]
|
|
|
|
else:
|
|
|
|
serialized_batches = []
|
|
|
|
|
|
|
|
while len(all_hosts) > 0:
|
|
|
|
play_hosts = []
|
|
|
|
for x in range(serial):
|
|
|
|
if len(all_hosts) > 0:
|
|
|
|
play_hosts.append(all_hosts.pop(0))
|
2014-10-15 22:53:43 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
serialized_batches.append(play_hosts)
|
2014-10-15 22:53:43 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
return serialized_batches
|
2015-07-21 18:51:53 +00:00
|
|
|
|
2016-01-26 19:11:28 +00:00
|
|
|
def _generate_retry_inventory(self, retry_path, replay_hosts):
|
|
|
|
'''
|
|
|
|
Called when a playbook run fails. It generates an inventory which 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.
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
2016-01-26 19:52:41 +00:00
|
|
|
with open(retry_path, 'w') as fd:
|
|
|
|
for x in replay_hosts:
|
|
|
|
fd.write("%s\n" % x)
|
2016-01-26 19:11:28 +00:00
|
|
|
except Exception as e:
|
|
|
|
display.error("Could not create retry file '%s'. The error was: %s" % (retry_path, e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|