2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 04:26:16 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00:00
|
|
|
# This file is part of Ansible
|
2012-02-24 04:26:16 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00: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 04:26:16 +00:00
|
|
|
|
2012-02-24 04:28:58 +00:00
|
|
|
import ansible.runner
|
2012-02-24 04:26:16 +00:00
|
|
|
import ansible.constants as C
|
2012-03-03 02:43:46 +00:00
|
|
|
from ansible.utils import *
|
2012-02-24 04:26:16 +00:00
|
|
|
import yaml
|
2012-02-05 18:05:09 +00:00
|
|
|
import shlex
|
2012-02-24 04:26:16 +00:00
|
|
|
|
|
|
|
# TODO: make a constants file rather than
|
|
|
|
# duplicating these
|
|
|
|
|
|
|
|
class PlayBook(object):
|
2012-03-03 03:03:03 +00:00
|
|
|
|
|
|
|
'''
|
2012-02-24 04:26:16 +00:00
|
|
|
runs an ansible playbook, given as a datastructure
|
2012-02-24 06:02:24 +00:00
|
|
|
or YAML filename. a playbook is a deployment, config
|
|
|
|
management, or automation based set of commands to
|
|
|
|
run in series.
|
|
|
|
|
|
|
|
multiple patterns do not execute simultaneously,
|
|
|
|
but tasks in each pattern do execute in parallel
|
|
|
|
according to the number of forks requested.
|
2012-02-24 04:26:16 +00:00
|
|
|
'''
|
|
|
|
|
2012-03-03 03:03:03 +00:00
|
|
|
def __init__(self,
|
2012-02-24 04:26:16 +00: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,
|
|
|
|
verbose=False):
|
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# TODO, once ansible-playbook is it's own script this will
|
|
|
|
# have much LESS parameters to the constructor and will
|
|
|
|
# read most everything per pattern from the playbook
|
|
|
|
# and this will be greatly simplified
|
|
|
|
|
2012-02-24 06:02:24 +00:00
|
|
|
self.host_list = host_list
|
|
|
|
self.module_path = module_path
|
|
|
|
self.forks = forks
|
|
|
|
self.timeout = timeout
|
|
|
|
self.remote_user = remote_user
|
|
|
|
self.remote_pass = remote_pass
|
|
|
|
self.verbose = verbose
|
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# store the list of changes/invocations/failure counts
|
|
|
|
# as a dictionary of integers keyed off the hostname
|
|
|
|
|
2012-02-25 20:21:11 +00:00
|
|
|
self.processed = {}
|
|
|
|
self.dark = {}
|
|
|
|
self.changed = {}
|
|
|
|
self.invocations = {}
|
|
|
|
self.failures = {}
|
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# playbook file can be passed in as a path or
|
|
|
|
# as file contents (to support API usage)
|
|
|
|
|
2012-02-24 06:02:24 +00:00
|
|
|
if type(playbook) == str:
|
|
|
|
playbook = yaml.load(file(playbook).read())
|
|
|
|
self.playbook = playbook
|
|
|
|
|
|
|
|
def run(self):
|
2012-02-27 01:18:42 +00:00
|
|
|
''' run all patterns in the playbook '''
|
2012-02-24 06:02:24 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# loop through all patterns and run them
|
2012-02-24 06:02:24 +00:00
|
|
|
for pattern in self.playbook:
|
|
|
|
self._run_pattern(pattern)
|
2012-02-25 20:21:11 +00:00
|
|
|
if self.verbose:
|
|
|
|
print "\n"
|
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# summarize the results
|
2012-02-25 20:21:11 +00:00
|
|
|
results = {}
|
|
|
|
for host in self.processed.keys():
|
|
|
|
results[host] = {
|
|
|
|
'resources' : self.invocations.get(host, 0),
|
|
|
|
'changed' : self.changed.get(host, 0),
|
|
|
|
'dark' : self.dark.get(host, 0),
|
|
|
|
'failed' : self.failures.get(host, 0)
|
2012-03-03 03:03:03 +00:00
|
|
|
}
|
2012-02-25 20:21:11 +00:00
|
|
|
return results
|
2012-02-24 04:26:16 +00:00
|
|
|
|
2012-03-03 02:43:46 +00:00
|
|
|
def _prune_failed_hosts(self, host_list):
|
|
|
|
new_hosts = []
|
|
|
|
for x in host_list:
|
2012-03-03 03:03:03 +00:00
|
|
|
if not x in self.failures and not x in self.dark:
|
2012-03-03 02:43:46 +00:00
|
|
|
new_hosts.append(x)
|
|
|
|
return new_hosts
|
|
|
|
|
|
|
|
def _run_module(self, pattern, module, args, hosts, remote_user):
|
|
|
|
''' run a particular module step in a playbook '''
|
|
|
|
return ansible.runner.Runner(
|
|
|
|
pattern=pattern,
|
|
|
|
module_name=module,
|
|
|
|
module_args=args,
|
|
|
|
host_list=hosts,
|
|
|
|
forks=self.forks,
|
|
|
|
remote_pass=self.remote_pass,
|
|
|
|
module_path=self.module_path,
|
|
|
|
timeout=self.timeout,
|
|
|
|
remote_user=remote_user
|
|
|
|
).run()
|
|
|
|
|
2012-03-03 03:03:03 +00:00
|
|
|
def _run_task(self, pattern=None, task=None, host_list=None,
|
2012-02-27 00:58:56 +00:00
|
|
|
remote_user=None, handlers=None, conditional=False):
|
2012-03-03 03:03:03 +00:00
|
|
|
'''
|
2012-02-24 06:02:24 +00:00
|
|
|
run a single task in the playbook and
|
|
|
|
recursively run any subtasks.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if host_list is None:
|
2012-02-27 01:18:42 +00:00
|
|
|
# pruned host lists occur when running triggered
|
|
|
|
# actions where not all hosts have changed
|
|
|
|
# though top-level tasks will pass in "None" here
|
2012-02-25 22:16:23 +00:00
|
|
|
host_list = self.host_list
|
2012-03-02 01:41:17 +00:00
|
|
|
(host_list, groups) = ansible.runner.Runner.parse_hosts(host_list)
|
2012-02-28 03:28:01 +00:00
|
|
|
|
|
|
|
# do not continue to run tasks on hosts that have had failures
|
2012-03-03 02:43:46 +00:00
|
|
|
host_list = self._prune_failed_hosts(host_list)
|
2012-02-24 06:02:24 +00:00
|
|
|
|
2012-03-03 02:43:46 +00:00
|
|
|
# load the module name and parameters from the task entry
|
2012-02-27 01:54:51 +00:00
|
|
|
name = task['name']
|
|
|
|
action = task['action']
|
|
|
|
comment = task.get('comment', '')
|
|
|
|
|
|
|
|
tokens = shlex.split(action)
|
2012-02-05 18:05:09 +00:00
|
|
|
module_name = tokens[0]
|
|
|
|
module_args = tokens[1:]
|
2012-02-24 07:36:38 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# tasks can be direct (run on all nodes matching
|
|
|
|
# the pattern) or conditional, where they ran
|
|
|
|
# as the result of a change handler on a subset
|
|
|
|
# of all of the hosts
|
|
|
|
|
2012-02-25 20:21:11 +00:00
|
|
|
if self.verbose:
|
2012-03-03 02:43:46 +00:00
|
|
|
print task_start_msg(name, conditional)
|
2012-02-24 07:36:38 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# load up an appropriate ansible runner to
|
|
|
|
# run the task in parallel
|
2012-03-03 03:03:03 +00:00
|
|
|
results = self._run_module(pattern, module_name,
|
|
|
|
module_args, host_list, remote_user)
|
2012-03-02 03:10:47 +00:00
|
|
|
|
|
|
|
# if no hosts are matched, carry on, unlike /bin/ansible
|
|
|
|
# which would warn you about this
|
|
|
|
if results is None:
|
|
|
|
results = {}
|
2012-02-24 06:02:24 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# walk through the results and build up
|
|
|
|
# summary information about successes and
|
|
|
|
# failures. TODO: split into subfunction
|
|
|
|
|
2012-03-03 02:43:46 +00:00
|
|
|
dark = results.get("dark", {})
|
2012-03-03 02:20:37 +00:00
|
|
|
contacted = results.get("contacted", {})
|
2012-03-03 02:43:46 +00:00
|
|
|
ok_hosts = contacted.keys()
|
2012-02-24 06:02:24 +00:00
|
|
|
|
|
|
|
for host, msg in dark.items():
|
2012-02-25 20:21:11 +00:00
|
|
|
self.processed[host] = 1
|
|
|
|
if self.verbose:
|
|
|
|
print "unreachable: [%s] => %s" % (host, msg)
|
2012-03-03 03:03:03 +00:00
|
|
|
if not host in self.dark:
|
2012-02-25 20:21:11 +00:00
|
|
|
self.dark[host] = 1
|
|
|
|
else:
|
|
|
|
self.dark[host] = self.dark[host] + 1
|
2012-02-24 07:36:38 +00:00
|
|
|
|
|
|
|
for host, results in contacted.items():
|
2012-02-25 20:21:11 +00:00
|
|
|
self.processed[host] = 1
|
|
|
|
|
2012-03-03 02:43:46 +00:00
|
|
|
if is_failed(results):
|
2012-02-25 20:21:11 +00:00
|
|
|
if self.verbose:
|
2012-03-03 02:43:46 +00:00
|
|
|
print "failed: [%s] => %s\n" % (host, smjson(results))
|
2012-03-03 03:03:03 +00:00
|
|
|
if not host in self.failures:
|
2012-02-25 20:21:11 +00:00
|
|
|
self.failures[host] = 1
|
|
|
|
else:
|
|
|
|
self.failures[host] = self.failures[host] + 1
|
|
|
|
else:
|
|
|
|
if self.verbose:
|
2012-03-03 02:43:46 +00:00
|
|
|
print "ok: [%s]\n" % host
|
2012-03-03 03:03:03 +00:00
|
|
|
if not host in self.invocations:
|
2012-02-25 20:21:11 +00:00
|
|
|
self.invocations[host] = 1
|
|
|
|
else:
|
|
|
|
self.invocations[host] = self.invocations[host] + 1
|
|
|
|
if results.get('changed', False):
|
2012-03-03 03:03:03 +00:00
|
|
|
if not host in self.changed:
|
2012-02-25 20:21:11 +00:00
|
|
|
self.changed[host] = 1
|
|
|
|
else:
|
2012-02-26 17:10:57 +00:00
|
|
|
self.changed[host] = self.changed[host] + 1
|
2012-02-25 20:21:11 +00:00
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
# flag which notify handlers need to be run
|
2012-02-27 01:18:42 +00:00
|
|
|
# this will be on a SUBSET of the actual host list. For instance
|
|
|
|
# a file might need to be written on only half of the nodes so
|
|
|
|
# we would only trigger restarting Apache on half of the nodes
|
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
subtasks = task.get('notify', [])
|
2012-02-24 06:02:24 +00:00
|
|
|
if len(subtasks) > 0:
|
2012-02-25 19:42:41 +00:00
|
|
|
for host, results in contacted.items():
|
2012-02-25 22:16:23 +00:00
|
|
|
if results.get('changed', False):
|
|
|
|
for subtask in subtasks:
|
2012-03-03 03:03:03 +00:00
|
|
|
self._flag_handler(handlers, subtask, host)
|
2012-02-24 06:02:24 +00:00
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
def _flag_handler(self, handlers, match_name, host):
|
|
|
|
'''
|
|
|
|
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 01:18:42 +00:00
|
|
|
|
|
|
|
# for all registered handlers in the ansible playbook
|
|
|
|
# for this particular pattern group
|
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
for x in handlers:
|
|
|
|
attribs = x["do"]
|
|
|
|
name = attribs[0]
|
|
|
|
if match_name == name:
|
2012-02-27 01:18:42 +00:00
|
|
|
# flag the handler with the list of hosts
|
|
|
|
# it needs to be run on, it will be run later
|
2012-03-03 03:03:03 +00:00
|
|
|
if not run in x:
|
2012-02-25 22:16:23 +00:00
|
|
|
x['run'] = []
|
|
|
|
x['run'].append(host)
|
2012-02-24 06:02:24 +00:00
|
|
|
|
|
|
|
def _run_pattern(self, pg):
|
|
|
|
'''
|
|
|
|
run a list of tasks for a given pattern, in order
|
|
|
|
'''
|
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# get configuration information about the pattern
|
2012-03-02 03:20:45 +00:00
|
|
|
pattern = pg['hosts']
|
2012-02-25 19:42:41 +00:00
|
|
|
tasks = pg['tasks']
|
|
|
|
handlers = pg['handlers']
|
2012-02-27 00:58:56 +00:00
|
|
|
user = pg.get('user', C.DEFAULT_REMOTE_USER)
|
2012-03-02 01:41:17 +00:00
|
|
|
|
2012-03-02 03:20:45 +00:00
|
|
|
host_file = pg.get('inventory', '/etc/ansible/hosts')
|
2012-03-02 01:41:17 +00:00
|
|
|
self.host_list, groups = ansible.runner.Runner.parse_hosts(host_file)
|
2012-02-25 20:21:11 +00:00
|
|
|
|
|
|
|
if self.verbose:
|
2012-03-03 03:03:03 +00:00
|
|
|
print "PLAY [%s] ****************************\n" % pattern
|
2012-02-25 20:21:11 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# run all the top level tasks, these get run on every node
|
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
for task in tasks:
|
2012-02-27 00:58:56 +00:00
|
|
|
self._run_task(
|
|
|
|
pattern=pattern,
|
|
|
|
task=task,
|
|
|
|
handlers=handlers,
|
2012-03-02 01:41:17 +00:00
|
|
|
remote_user=user
|
|
|
|
)
|
2012-02-27 01:18:42 +00:00
|
|
|
|
|
|
|
# handlers only run on certain nodes, they are flagged by _flag_handlers
|
|
|
|
# above. They only run on nodes when things mark them as changed, and
|
|
|
|
# handlers only get run once. For instance, the system is designed
|
|
|
|
# such that multiple config files if changed can ask for an Apache restart
|
|
|
|
# but Apache will only be restarted once (at the end).
|
|
|
|
|
2012-02-25 19:42:41 +00:00
|
|
|
for task in handlers:
|
|
|
|
if type(task.get("run", None)) == list:
|
|
|
|
self._run_task(
|
|
|
|
pattern=pattern,
|
2012-03-03 03:03:03 +00:00
|
|
|
task=task,
|
2012-02-25 19:42:41 +00:00
|
|
|
handlers=handlers,
|
|
|
|
host_list=task.get('run',[]),
|
2012-02-27 00:58:56 +00:00
|
|
|
conditional=True,
|
|
|
|
remote_user=user
|
2012-02-25 19:42:41 +00:00
|
|
|
)
|
2012-02-24 04:26:16 +00:00
|
|
|
|
2012-02-27 01:18:42 +00:00
|
|
|
# end of execution for this particular pattern. Multiple patterns
|
|
|
|
# can be in a single playbook file
|
|
|
|
|
2012-02-24 04:26:16 +00:00
|
|
|
|
|
|
|
|