2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, 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:28:58 +01:00
|
|
|
import ansible.runner
|
2012-02-24 05:26:16 +01:00
|
|
|
import ansible.constants as C
|
2012-03-18 22:16:12 +01:00
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
2012-03-03 16:53:15 +01:00
|
|
|
import os
|
2012-07-15 16:57:22 +02:00
|
|
|
import collections
|
2012-05-26 06:37:34 +02:00
|
|
|
from play import Play
|
2012-03-06 02:09:03 +01:00
|
|
|
|
2012-07-26 02:45:51 +02:00
|
|
|
SETUP_CACHE = collections.defaultdict(dict)
|
|
|
|
|
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,
|
|
|
|
module_path = C.DEFAULT_MODULE_PATH,
|
|
|
|
forks = C.DEFAULT_FORKS,
|
|
|
|
timeout = C.DEFAULT_TIMEOUT,
|
|
|
|
remote_user = C.DEFAULT_REMOTE_USER,
|
|
|
|
remote_pass = C.DEFAULT_REMOTE_PASS,
|
2012-04-14 01:06:11 +02:00
|
|
|
sudo_pass = C.DEFAULT_SUDO_PASS,
|
2012-03-28 23:05:31 +02:00
|
|
|
remote_port = C.DEFAULT_REMOTE_PORT,
|
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-04-27 01:56:10 +02:00
|
|
|
sudo = False,
|
2012-05-07 17:37:50 +02:00
|
|
|
sudo_user = C.DEFAULT_SUDO_USER,
|
2012-07-12 01:51:26 +02:00
|
|
|
extra_vars = None,
|
2012-08-10 08:45:29 +02:00
|
|
|
only_tags = None,
|
|
|
|
subset = C.DEFAULT_SUBSET):
|
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/
|
|
|
|
forks: desired level of paralellism
|
|
|
|
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
|
|
|
|
sudo_pass: if sudo==True, and a password is required, this is the sudo password
|
|
|
|
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
|
|
|
|
stats: holds aggregrate data about events occuring to each host
|
|
|
|
sudo: if not specified per play, requests all plays use sudo mode
|
|
|
|
"""
|
|
|
|
|
2012-07-26 02:45:51 +02:00
|
|
|
self.SETUP_CACHE = SETUP_CACHE
|
2012-06-01 04:08:00 +02:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
if playbook is None or callbacks is None or runner_callbacks is None or stats is None:
|
|
|
|
raise Exception('missing required arguments')
|
|
|
|
|
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' ]
|
2012-08-07 02:07:02 +02:00
|
|
|
|
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-17 04:15:55 +02:00
|
|
|
self.sudo = sudo
|
2012-04-14 01:06:11 +02:00
|
|
|
self.sudo_pass = sudo_pass
|
2012-05-07 00:24:04 +02:00
|
|
|
self.sudo_user = sudo_user
|
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
|
2012-03-03 16:53:15 +01:00
|
|
|
|
2012-08-10 08:45:29 +02:00
|
|
|
self.inventory = ansible.inventory.Inventory(host_list)
|
|
|
|
self.inventory.subset(subset)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-05-08 05:16:20 +02:00
|
|
|
if not self.inventory._is_script:
|
|
|
|
self.global_vars.update(self.inventory.get_group_variables('all'))
|
2012-05-03 00:29:46 +02:00
|
|
|
|
2012-07-15 17:20:59 +02:00
|
|
|
self.basedir = os.path.dirname(playbook)
|
|
|
|
self.playbook = self._load_playbook_from_file(playbook)
|
2012-06-25 20:57:09 +02:00
|
|
|
self.module_path = self.module_path + os.pathsep + os.path.join(self.basedir, "library")
|
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
2012-07-12 06:17:51 +02:00
|
|
|
|
|
|
|
def _load_playbook_from_file(self, path):
|
|
|
|
'''
|
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
|
|
|
'''
|
|
|
|
|
|
|
|
playbook_data = utils.parse_yaml_from_file(path)
|
|
|
|
accumulated_plays = []
|
|
|
|
|
|
|
|
if type(playbook_data) != list:
|
2012-07-15 15:32:47 +02:00
|
|
|
raise errors.AnsibleError("parse error: playbooks must be formatted as a YAML list")
|
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:
|
|
|
|
raise errors.AnsibleError("parse error: each play in a playbook must a YAML dictionary (hash), recieved: %s" % play)
|
|
|
|
if 'include' in play:
|
|
|
|
if len(play.keys()) == 1:
|
|
|
|
included_path = utils.path_dwim(self.basedir, play['include'])
|
|
|
|
accumulated_plays.extend(self._load_playbook_from_file(included_path))
|
|
|
|
else:
|
|
|
|
raise errors.AnsibleError("parse error: top level includes cannot be used with other directives: %s" % play)
|
|
|
|
else:
|
|
|
|
accumulated_plays.append(play)
|
2012-07-12 06:17:51 +02:00
|
|
|
|
|
|
|
return accumulated_plays
|
|
|
|
|
|
|
|
# *****************************************************
|
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-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-05-26 06:37:34 +02:00
|
|
|
for play_ds in self.playbook:
|
|
|
|
self._run_play(Play(self,play_ds))
|
2012-02-25 21:21:11 +01: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' }
|
|
|
|
self.runner_callbacks.on_failed(host, reason)
|
|
|
|
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
|
|
|
# *****************************************************
|
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
def _run_task_internal(self, task):
|
2012-03-03 03:43:46 +01:00
|
|
|
''' run a particular module step in a playbook '''
|
2012-03-24 01:51:15 +01:00
|
|
|
|
2012-04-13 14:39:54 +02:00
|
|
|
hosts = [ h for h in self.inventory.list_hosts() if (h not in self.stats.failures) and (h not in self.stats.dark)]
|
|
|
|
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(
|
2012-05-26 06:37:34 +02:00
|
|
|
pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name,
|
|
|
|
module_args=task.module_args, forks=self.forks,
|
2012-03-24 01:51:15 +01:00
|
|
|
remote_pass=self.remote_pass, module_path=self.module_path,
|
2012-08-07 02:07:02 +02:00
|
|
|
timeout=self.timeout, remote_user=task.play.remote_user,
|
2012-05-26 06:37:34 +02:00
|
|
|
remote_port=task.play.remote_port, module_vars=task.module_vars,
|
2012-05-14 22:52:48 +02:00
|
|
|
private_key_file=self.private_key_file,
|
2012-05-26 06:37:34 +02:00
|
|
|
setup_cache=self.SETUP_CACHE, basedir=self.basedir,
|
2012-08-07 02:07:02 +02:00
|
|
|
conditional=task.only_if, callbacks=self.runner_callbacks,
|
2012-08-09 03:09:14 +02:00
|
|
|
sudo=task.play.sudo, sudo_user=task.play.sudo_user,
|
2012-05-26 06:37:34 +02:00
|
|
|
transport=task.play.transport, sudo_pass=self.sudo_pass, is_playbook=True
|
2012-03-13 01:53:10 +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)
|
2012-04-13 14:39:54 +02:00
|
|
|
|
|
|
|
self.inventory.lift_restriction()
|
|
|
|
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
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
self.callbacks.on_task_start(task.name, is_handler)
|
2012-02-24 08:36:38 +01:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
# load up an appropriate ansible runner to run the task in parallel
|
|
|
|
results = self._run_task_internal(task)
|
2012-07-15 17:20:59 +02:00
|
|
|
# if no hosts are matched, carry on
|
|
|
|
if results is None:
|
|
|
|
results = {}
|
2012-03-02 04:10:47 +01:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
self.stats.compute(results)
|
2012-07-26 03:30:49 +02:00
|
|
|
|
|
|
|
# add facts to the global setup cache
|
|
|
|
for host, result in results['contacted'].iteritems():
|
|
|
|
facts = result.get('ansible_facts', {})
|
|
|
|
self.SETUP_CACHE[host].update(facts)
|
2012-08-10 07:13:37 +02:00
|
|
|
if task.register:
|
|
|
|
self.SETUP_CACHE[host][task.register] = result
|
2012-08-07 02:07:02 +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:
|
2012-06-08 01:25:47 +02:00
|
|
|
self._flag_handler(play.handlers(), utils.template(handler_name, task.module_vars), host)
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2012-03-24 01:51:15 +01:00
|
|
|
# *****************************************************
|
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
def _flag_handler(self, handlers, 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-02-25 20:42:41 +01:00
|
|
|
for x in handlers:
|
2012-05-26 06:37:34 +02:00
|
|
|
if handler_name == x.name:
|
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
|
|
|
|
|
|
|
host_list = [ h for h in self.inventory.list_hosts(play.hosts)
|
2012-07-15 01:49:42 +02:00
|
|
|
if not (h in self.stats.failures or h in self.stats.dark) ]
|
|
|
|
|
|
|
|
if not play.gather_facts:
|
|
|
|
return {}
|
2012-03-20 03:42:31 +01:00
|
|
|
|
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)
|
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(
|
2012-07-15 17:20:59 +02:00
|
|
|
pattern=play.hosts, module_name='setup', module_args={}, inventory=self.inventory,
|
2012-05-26 06:37:34 +02:00
|
|
|
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,
|
2012-08-07 02:07:02 +02:00
|
|
|
setup_cache=self.SETUP_CACHE, callbacks=self.runner_callbacks, sudo=play.sudo, sudo_user=play.sudo_user,
|
2012-08-09 03:09:14 +02:00
|
|
|
transport=play.transport, sudo_pass=self.sudo_pass, is_playbook=True
|
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():
|
2012-07-15 17:20:59 +02:00
|
|
|
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
|
|
|
# *****************************************************
|
|
|
|
|
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 '''
|
2012-03-20 03:42:31 +01:00
|
|
|
|
2012-07-12 02:30:30 +02:00
|
|
|
if not play.should_run(self.only_tags):
|
|
|
|
return
|
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
self.callbacks.on_play_start(play.name)
|
2012-03-20 03:42:31 +01:00
|
|
|
|
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!
|
2012-08-14 05:36:51 +02:00
|
|
|
play.update_vars_files(self.inventory.list_hosts(play.hosts))
|
2012-03-20 03:42:31 +01:00
|
|
|
|
2012-05-26 06:37:34 +02:00
|
|
|
for task in play.tasks():
|
2012-07-12 01:51:26 +02:00
|
|
|
# only run the task if the requested tags match
|
|
|
|
should_run = False
|
|
|
|
for x in self.only_tags:
|
|
|
|
for y in task.tags:
|
|
|
|
if (x==y):
|
|
|
|
should_run = True
|
|
|
|
break
|
|
|
|
if should_run:
|
|
|
|
self._run_task(play, task, False)
|
2012-05-26 06:37:34 +02:00
|
|
|
|
|
|
|
# run notify actions
|
|
|
|
for handler in play.handlers():
|
|
|
|
if len(handler.notified_by) > 0:
|
|
|
|
self.inventory.restrict_to(handler.notified_by)
|
|
|
|
self._run_task(play, handler, True)
|
2012-04-13 14:39:54 +02:00
|
|
|
self.inventory.lift_restriction()
|