2012-02-24 05:26:16 +01:00
|
|
|
# Copyright (c) 2012 Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person
|
|
|
|
# obtaining a copy of this software and associated documentation
|
|
|
|
# files (the "Software"), to deal in the Software without restriction,
|
|
|
|
# including without limitation the rights to use, copy, modify, merge,
|
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
# and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be
|
|
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
|
|
|
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
|
|
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2012-02-24 05:28:58 +01:00
|
|
|
import ansible.runner
|
2012-02-24 05:26:16 +01:00
|
|
|
import ansible.constants as C
|
|
|
|
import yaml
|
2012-02-05 19:05:09 +01:00
|
|
|
import shlex
|
2012-02-24 05:26:16 +01:00
|
|
|
|
|
|
|
# TODO: make a constants file rather than
|
|
|
|
# duplicating these
|
|
|
|
|
|
|
|
class PlayBook(object):
|
|
|
|
'''
|
|
|
|
runs an ansible playbook, given as a datastructure
|
2012-02-24 07:02:24 +01: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 05:26:16 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
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 02:18:42 +01: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 07:02:24 +01: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 02:18:42 +01:00
|
|
|
# store the list of changes/invocations/failure counts
|
|
|
|
# as a dictionary of integers keyed off the hostname
|
|
|
|
|
2012-02-25 21:21:11 +01:00
|
|
|
self.processed = {}
|
|
|
|
self.dark = {}
|
|
|
|
self.changed = {}
|
|
|
|
self.invocations = {}
|
|
|
|
self.failures = {}
|
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# playbook file can be passed in as a path or
|
|
|
|
# as file contents (to support API usage)
|
|
|
|
|
2012-02-24 07:02:24 +01:00
|
|
|
if type(playbook) == str:
|
|
|
|
playbook = yaml.load(file(playbook).read())
|
|
|
|
self.playbook = playbook
|
|
|
|
|
|
|
|
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-02-24 07:02:24 +01:00
|
|
|
for pattern in self.playbook:
|
|
|
|
self._run_pattern(pattern)
|
2012-02-25 21:21:11 +01:00
|
|
|
if self.verbose:
|
|
|
|
print "\n"
|
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# summarize the results
|
2012-02-25 21:21:11 +01: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)
|
|
|
|
}
|
|
|
|
return results
|
2012-02-24 05:26:16 +01:00
|
|
|
|
2012-02-27 01:58:56 +01:00
|
|
|
def _run_task(self, pattern=None, task=None, host_list=None,
|
|
|
|
remote_user=None, handlers=None, conditional=False):
|
2012-02-24 07:02:24 +01:00
|
|
|
'''
|
|
|
|
run a single task in the playbook and
|
|
|
|
recursively run any subtasks.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if host_list is None:
|
2012-02-27 02:18:42 +01: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 23:16:23 +01:00
|
|
|
host_list = self.host_list
|
2012-02-28 04:28:01 +01:00
|
|
|
host_list = ansible.runner.Runner.parse_hosts(host_list)
|
|
|
|
|
|
|
|
# do not continue to run tasks on hosts that have had failures
|
|
|
|
new_hosts = []
|
|
|
|
for x in host_list:
|
|
|
|
if not self.failures.has_key(x):
|
|
|
|
new_hosts.append(x)
|
|
|
|
host_list = new_hosts
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# load the module name and parameters from the task
|
|
|
|
# entry
|
2012-02-27 02:54:51 +01:00
|
|
|
name = task['name']
|
|
|
|
action = task['action']
|
|
|
|
comment = task.get('comment', '')
|
|
|
|
|
|
|
|
tokens = shlex.split(action)
|
2012-02-05 19:05:09 +01:00
|
|
|
module_name = tokens[0]
|
|
|
|
module_args = tokens[1:]
|
2012-02-24 08:36:38 +01:00
|
|
|
|
2012-02-27 02:18:42 +01: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 21:21:11 +01:00
|
|
|
if self.verbose:
|
|
|
|
if not conditional:
|
2012-02-27 02:54:51 +01:00
|
|
|
print "\nTASK [%s]" % (name)
|
2012-02-25 21:21:11 +01:00
|
|
|
else:
|
2012-02-27 02:54:51 +01:00
|
|
|
print "\nNOTIFIED [%s]" % (name)
|
2012-02-24 08:36:38 +01:00
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# load up an appropriate ansible runner to
|
|
|
|
# run the task in parallel
|
|
|
|
|
2012-02-27 01:55:26 +01:00
|
|
|
runner = ansible.runner.Runner(
|
2012-02-24 08:14:22 +01:00
|
|
|
pattern=pattern,
|
2012-02-24 07:02:24 +01:00
|
|
|
module_name=module_name,
|
2012-02-27 01:55:26 +01:00
|
|
|
module_args=module_args,
|
|
|
|
host_list=host_list,
|
|
|
|
forks=self.forks,
|
|
|
|
remote_pass=self.remote_pass,
|
|
|
|
module_path=self.module_path,
|
2012-02-27 01:58:56 +01:00
|
|
|
timeout=self.timeout,
|
|
|
|
remote_user=remote_user
|
2012-02-24 07:02:24 +01:00
|
|
|
)
|
|
|
|
results = runner.run()
|
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# walk through the results and build up
|
|
|
|
# summary information about successes and
|
|
|
|
# failures. TODO: split into subfunction
|
|
|
|
|
2012-02-24 07:02:24 +01:00
|
|
|
dark = results.get("dark", [])
|
|
|
|
contacted = results.get("contacted", [])
|
|
|
|
ok_hosts = contacted.keys()
|
|
|
|
|
|
|
|
for host, msg in dark.items():
|
2012-02-25 21:21:11 +01:00
|
|
|
self.processed[host] = 1
|
|
|
|
if self.verbose:
|
|
|
|
print "unreachable: [%s] => %s" % (host, msg)
|
|
|
|
if not self.dark.has_key(host):
|
|
|
|
self.dark[host] = 1
|
|
|
|
else:
|
|
|
|
self.dark[host] = self.dark[host] + 1
|
2012-02-24 08:36:38 +01:00
|
|
|
|
|
|
|
for host, results in contacted.items():
|
2012-02-25 21:21:11 +01:00
|
|
|
self.processed[host] = 1
|
|
|
|
failed = False
|
2012-02-24 08:36:38 +01:00
|
|
|
if module_name == "command":
|
2012-02-25 23:16:23 +01:00
|
|
|
if results.get("rc", 0) != 0:
|
|
|
|
failed=True
|
2012-02-24 08:36:38 +01:00
|
|
|
elif results.get("failed", 0) == 1:
|
2012-02-25 23:16:23 +01:00
|
|
|
failed=True
|
2012-02-25 21:21:11 +01:00
|
|
|
|
|
|
|
if failed:
|
|
|
|
if self.verbose:
|
|
|
|
print "failure: [%s] => %s" % (host, results)
|
|
|
|
if not self.failures.has_key(host):
|
|
|
|
self.failures[host] = 1
|
|
|
|
else:
|
|
|
|
self.failures[host] = self.failures[host] + 1
|
|
|
|
else:
|
|
|
|
if self.verbose:
|
|
|
|
print "ok: [%s]" % host
|
|
|
|
if not self.invocations.has_key(host):
|
|
|
|
self.invocations[host] = 1
|
|
|
|
else:
|
|
|
|
self.invocations[host] = self.invocations[host] + 1
|
|
|
|
if results.get('changed', False):
|
|
|
|
if not self.changed.has_key(host):
|
|
|
|
self.changed[host] = 1
|
|
|
|
else:
|
2012-02-26 18:10:57 +01:00
|
|
|
self.changed[host] = self.changed[host] + 1
|
2012-02-25 21:21:11 +01:00
|
|
|
|
2012-02-25 20:42:41 +01:00
|
|
|
# flag which notify handlers need to be run
|
2012-02-27 02:18:42 +01: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 20:42:41 +01:00
|
|
|
subtasks = task.get('notify', [])
|
2012-02-24 07:02:24 +01:00
|
|
|
if len(subtasks) > 0:
|
2012-02-25 20:42:41 +01:00
|
|
|
for host, results in contacted.items():
|
2012-02-25 23:16:23 +01:00
|
|
|
if results.get('changed', False):
|
|
|
|
for subtask in subtasks:
|
|
|
|
self._flag_handler(handlers, subtask, host)
|
2012-02-24 07:02:24 +01:00
|
|
|
|
2012-02-25 20:42:41 +01: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 02:18:42 +01:00
|
|
|
|
|
|
|
# for all registered handlers in the ansible playbook
|
|
|
|
# for this particular pattern group
|
|
|
|
|
2012-02-25 20:42:41 +01:00
|
|
|
for x in handlers:
|
|
|
|
attribs = x["do"]
|
|
|
|
name = attribs[0]
|
|
|
|
if match_name == name:
|
2012-02-27 02:18:42 +01:00
|
|
|
# flag the handler with the list of hosts
|
|
|
|
# it needs to be run on, it will be run later
|
2012-02-25 23:16:23 +01:00
|
|
|
if not x.has_key("run"):
|
|
|
|
x['run'] = []
|
|
|
|
x['run'].append(host)
|
2012-02-24 07:02:24 +01:00
|
|
|
|
|
|
|
def _run_pattern(self, pg):
|
|
|
|
'''
|
|
|
|
run a list of tasks for a given pattern, in order
|
|
|
|
'''
|
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# get configuration information about the pattern
|
2012-02-25 20:42:41 +01:00
|
|
|
pattern = pg['pattern']
|
|
|
|
tasks = pg['tasks']
|
|
|
|
handlers = pg['handlers']
|
2012-02-27 01:58:56 +01:00
|
|
|
user = pg.get('user', C.DEFAULT_REMOTE_USER)
|
2012-02-25 21:21:11 +01:00
|
|
|
self.host_list = pg.get('hosts', '/etc/ansible/hosts')
|
|
|
|
|
|
|
|
if self.verbose:
|
|
|
|
print "PLAY: [%s] from [%s] ********** " % (pattern, self.host_list)
|
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# run all the top level tasks, these get run on every node
|
|
|
|
|
2012-02-25 20:42:41 +01:00
|
|
|
for task in tasks:
|
2012-02-27 01:58:56 +01:00
|
|
|
self._run_task(
|
|
|
|
pattern=pattern,
|
|
|
|
task=task,
|
|
|
|
handlers=handlers,
|
|
|
|
remote_user=user)
|
2012-02-27 02:18:42 +01: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 20:42:41 +01:00
|
|
|
for task in handlers:
|
|
|
|
if type(task.get("run", None)) == list:
|
|
|
|
self._run_task(
|
|
|
|
pattern=pattern,
|
|
|
|
task=task,
|
|
|
|
handlers=handlers,
|
|
|
|
host_list=task.get('run',[]),
|
2012-02-27 01:58:56 +01:00
|
|
|
conditional=True,
|
|
|
|
remote_user=user
|
2012-02-25 20:42:41 +01:00
|
|
|
)
|
2012-02-24 05:26:16 +01:00
|
|
|
|
2012-02-27 02:18:42 +01:00
|
|
|
# end of execution for this particular pattern. Multiple patterns
|
|
|
|
# can be in a single playbook file
|
|
|
|
|
2012-02-24 05:26:16 +01:00
|
|
|
|
|
|
|
|