2012-03-22 03:18:57 +01:00
|
|
|
# (C) 2012, 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/>.
|
|
|
|
|
|
|
|
import utils
|
2012-03-30 22:17:16 +02:00
|
|
|
import sys
|
2012-04-02 23:46:02 +02:00
|
|
|
import getpass
|
2012-05-26 17:52:01 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
2012-08-19 01:43:08 +02:00
|
|
|
import os.path
|
2012-07-25 01:30:02 +02:00
|
|
|
from ansible.color import stringc
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-08-19 01:43:08 +02:00
|
|
|
dirname = os.path.dirname(__file__)
|
2012-08-22 02:32:21 +02:00
|
|
|
callbacks = utils.import_plugins(os.path.join(dirname, 'callback_plugins'))
|
2012-09-07 00:57:04 +02:00
|
|
|
callbacks = [ c.CallbackModule() for c in callbacks.values() ]
|
2012-08-19 01:43:08 +02:00
|
|
|
|
2012-07-15 18:29:53 +02:00
|
|
|
cowsay = None
|
|
|
|
if os.path.exists("/usr/bin/cowsay"):
|
|
|
|
cowsay = "/usr/bin/cowsay"
|
|
|
|
elif os.path.exists("/usr/games/cowsay"):
|
|
|
|
cowsay = "/usr/games/cowsay"
|
2012-10-06 16:56:19 +02:00
|
|
|
if os.getenv("ANSIBLE_NOCOWS") is not None:
|
|
|
|
cowsay = None
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-08-19 01:43:08 +02:00
|
|
|
def call_callback_module(method_name, *args, **kwargs):
|
|
|
|
|
|
|
|
for callback_plugin in callbacks:
|
|
|
|
methods = [
|
|
|
|
getattr(callback_plugin, method_name, None),
|
|
|
|
getattr(callback_plugin, 'on_any', None)
|
|
|
|
]
|
|
|
|
for method in methods:
|
|
|
|
if method is not None:
|
|
|
|
method(*args, **kwargs)
|
|
|
|
|
2012-08-09 03:16:48 +02:00
|
|
|
def vv(msg, host=None):
|
|
|
|
return verbose(msg, host=host, caplevel=1)
|
|
|
|
|
2012-08-09 03:09:14 +02:00
|
|
|
def vvv(msg, host=None):
|
2012-08-09 03:16:48 +02:00
|
|
|
return verbose(msg, host=host, caplevel=2)
|
|
|
|
|
|
|
|
def verbose(msg, host=None, caplevel=2):
|
|
|
|
if utils.VERBOSITY > caplevel:
|
2012-08-09 03:09:14 +02:00
|
|
|
if host is None:
|
|
|
|
print stringc(msg, 'blue')
|
|
|
|
else:
|
|
|
|
print stringc("<%s> %s" % (host, msg), 'blue')
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
class AggregateStats(object):
|
2012-08-07 02:07:02 +02:00
|
|
|
''' holds stats about per-host activity during playbook runs '''
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
def __init__(self):
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
self.processed = {}
|
|
|
|
self.failures = {}
|
|
|
|
self.ok = {}
|
|
|
|
self.dark = {}
|
|
|
|
self.changed = {}
|
|
|
|
self.skipped = {}
|
|
|
|
|
|
|
|
def _increment(self, what, host):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' helper function to bump a statistic '''
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
self.processed[host] = 1
|
|
|
|
prev = (getattr(self, what)).get(host, 0)
|
|
|
|
getattr(self, what)[host] = prev+1
|
|
|
|
|
2012-09-04 22:40:07 +02:00
|
|
|
def compute(self, runner_results, setup=False, poll=False, ignore_errors=False):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' walk through all results and increment stats '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
for (host, value) in runner_results.get('contacted', {}).iteritems():
|
2012-09-04 22:40:07 +02:00
|
|
|
if not ignore_errors and (('failed' in value and bool(value['failed'])) or
|
|
|
|
('rc' in value and value['rc'] != 0)):
|
2012-03-26 01:05:27 +02:00
|
|
|
self._increment('failures', host)
|
|
|
|
elif 'skipped' in value and bool(value['skipped']):
|
|
|
|
self._increment('skipped', host)
|
|
|
|
elif 'changed' in value and bool(value['changed']):
|
2012-04-26 20:34:49 +02:00
|
|
|
if not setup and not poll:
|
2012-03-26 01:05:27 +02:00
|
|
|
self._increment('changed', host)
|
|
|
|
self._increment('ok', host)
|
|
|
|
else:
|
|
|
|
if not poll or ('finished' in value and bool(value['finished'])):
|
|
|
|
self._increment('ok', host)
|
|
|
|
|
|
|
|
for (host, value) in runner_results.get('dark', {}).iteritems():
|
|
|
|
self._increment('dark', host)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
|
|
|
|
def summarize(self, host):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' return information about a particular host '''
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
return dict(
|
|
|
|
ok = self.ok.get(host, 0),
|
|
|
|
failures = self.failures.get(host, 0),
|
|
|
|
unreachable = self.dark.get(host,0),
|
|
|
|
changed = self.changed.get(host, 0),
|
|
|
|
skipped = self.skipped.get(host, 0)
|
|
|
|
)
|
|
|
|
|
2012-03-27 04:05:11 +02:00
|
|
|
########################################################################
|
|
|
|
|
2012-07-15 16:12:49 +02:00
|
|
|
def regular_generic_msg(hostname, result, oneline, caption):
|
|
|
|
''' output on the result of a module run that is not command '''
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-07-15 16:12:49 +02:00
|
|
|
if not oneline:
|
|
|
|
return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result,format=True))
|
|
|
|
else:
|
|
|
|
return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result))
|
|
|
|
|
|
|
|
|
2012-05-26 17:52:01 +02:00
|
|
|
def banner(msg):
|
2012-07-12 11:14:15 +02:00
|
|
|
|
2012-07-15 18:29:53 +02:00
|
|
|
if cowsay != None:
|
2012-08-30 00:11:40 +02:00
|
|
|
cmd = subprocess.Popen([cowsay, "-W", "60", msg],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-05-26 17:52:01 +02:00
|
|
|
(out, err) = cmd.communicate()
|
2012-08-07 02:07:02 +02:00
|
|
|
return "%s\n" % out
|
2012-05-26 17:52:01 +02:00
|
|
|
else:
|
2012-07-15 18:29:53 +02:00
|
|
|
return "\n%s ********************* " % msg
|
2012-07-15 16:12:49 +02:00
|
|
|
|
|
|
|
def command_generic_msg(hostname, result, oneline, caption):
|
|
|
|
''' output the result of a command run '''
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-07-15 16:12:49 +02:00
|
|
|
rc = result.get('rc', '0')
|
|
|
|
stdout = result.get('stdout','')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
|
msg = result.get('msg', '')
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-08-20 21:50:44 +02:00
|
|
|
hostname = hostname.encode('utf-8')
|
|
|
|
caption = caption.encode('utf-8')
|
|
|
|
|
2012-07-15 16:12:49 +02:00
|
|
|
if not oneline:
|
|
|
|
buf = "%s | %s | rc=%s >>\n" % (hostname, caption, result.get('rc',0))
|
|
|
|
if stdout:
|
|
|
|
buf += stdout
|
|
|
|
if stderr:
|
|
|
|
buf += stderr
|
|
|
|
if msg:
|
|
|
|
buf += msg
|
2012-07-15 18:29:53 +02:00
|
|
|
return buf + "\n"
|
2012-07-15 16:12:49 +02:00
|
|
|
else:
|
|
|
|
if stderr:
|
|
|
|
return "%s | %s | rc=%s | (stdout) %s (stderr) %s\n" % (hostname, caption, rc, stdout, stderr)
|
|
|
|
else:
|
|
|
|
return "%s | %s | rc=%s | (stdout) %s\n" % (hostname, caption, rc, stdout)
|
|
|
|
|
|
|
|
def host_report_msg(hostname, module_name, result, oneline):
|
|
|
|
''' summarize the JSON results for a particular host '''
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-07-15 16:12:49 +02:00
|
|
|
failed = utils.is_failed(result)
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = ''
|
2012-08-17 03:44:59 +02:00
|
|
|
if module_name in [ 'command', 'shell', 'raw' ] and 'ansible_job_id' not in result and result.get('parsed',True) != False:
|
2012-07-15 16:12:49 +02:00
|
|
|
if not failed:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = command_generic_msg(hostname, result, oneline, 'success')
|
2012-07-15 16:12:49 +02:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = command_generic_msg(hostname, result, oneline, 'FAILED')
|
2012-07-15 16:12:49 +02:00
|
|
|
else:
|
|
|
|
if not failed:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = regular_generic_msg(hostname, result, oneline, 'success')
|
2012-07-15 16:12:49 +02:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = regular_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
return msg
|
2012-07-15 16:12:49 +02:00
|
|
|
|
|
|
|
###############################################
|
2012-05-26 17:52:01 +02:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
class DefaultRunnerCallbacks(object):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' no-op callbacks for API usage of Runner() if no callbacks are specified '''
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-03-22 03:18:57 +01:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2012-08-01 21:17:16 +02:00
|
|
|
def on_failed(self, host, res, ignore_errors=False):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_failed', host, res, ignore_errors=ignore_errors)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
def on_ok(self, host, res):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_ok', host, res)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-03-31 17:45:29 +02:00
|
|
|
def on_error(self, host, msg):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_error', host, msg)
|
2012-03-31 17:45:29 +02:00
|
|
|
|
2012-08-19 01:43:08 +02:00
|
|
|
def on_skipped(self, host, item=None):
|
|
|
|
call_callback_module('runner_on_skipped', host, item=item)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
def on_unreachable(self, host, res):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_unreachable', host, res)
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-04-12 03:05:46 +02:00
|
|
|
def on_no_hosts(self):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_no_hosts')
|
2012-04-12 03:05:46 +02:00
|
|
|
|
2012-04-26 20:34:49 +02:00
|
|
|
def on_async_poll(self, host, res, jid, clock):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_async_poll', host, res, jid, clock)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_ok(self, host, res, jid):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_async_ok', host, res, jid)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_failed(self, host, res, jid):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('runner_on_async_failed', host, res, jid)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
2012-03-27 04:05:11 +02:00
|
|
|
########################################################################
|
|
|
|
|
2012-03-27 03:17:11 +02:00
|
|
|
class CliRunnerCallbacks(DefaultRunnerCallbacks):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' callbacks for use by /usr/bin/ansible '''
|
2012-03-27 03:17:11 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# set by /usr/bin/ansible later
|
2012-08-07 02:07:02 +02:00
|
|
|
self.options = None
|
2012-04-30 09:47:04 +02:00
|
|
|
self._async_notified = {}
|
2012-03-27 03:17:11 +02:00
|
|
|
|
2012-08-01 21:17:16 +02:00
|
|
|
def on_failed(self, host, res, ignore_errors=False):
|
2012-06-20 03:55:57 +02:00
|
|
|
self._on_any(host,res)
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_failed(host, res, ignore_errors=ignore_errors)
|
2012-03-27 03:17:11 +02:00
|
|
|
|
|
|
|
def on_ok(self, host, res):
|
2012-06-20 03:55:57 +02:00
|
|
|
self._on_any(host,res)
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_ok(host, res)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-27 03:17:11 +02:00
|
|
|
def on_unreachable(self, host, res):
|
2012-05-26 00:44:29 +02:00
|
|
|
if type(res) == dict:
|
|
|
|
res = res.get('msg','')
|
2012-03-27 03:17:11 +02:00
|
|
|
print "%s | FAILED => %s" % (host, res)
|
|
|
|
if self.options.tree:
|
2012-07-15 16:12:49 +02:00
|
|
|
utils.write_tree_file(
|
2012-08-07 02:07:02 +02:00
|
|
|
self.options.tree, host,
|
2012-07-15 16:12:49 +02:00
|
|
|
utils.jsonify(dict(failed=True, msg=res),format=True)
|
|
|
|
)
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_unreachable(host, res)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-09-05 03:56:32 +02:00
|
|
|
def on_skipped(self, host, item=None):
|
2012-09-30 17:54:20 +02:00
|
|
|
print "%s | skipped" % (host)
|
2012-09-05 03:56:32 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_skipped(host, item)
|
2012-03-27 03:17:11 +02:00
|
|
|
|
2012-03-31 17:45:29 +02:00
|
|
|
def on_error(self, host, err):
|
2012-04-27 07:25:38 +02:00
|
|
|
print >>sys.stderr, "err: [%s] => %s\n" % (host, err)
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_error(host, err)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-04-12 03:05:46 +02:00
|
|
|
def on_no_hosts(self):
|
|
|
|
print >>sys.stderr, "no hosts matched\n"
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_no_hosts()
|
2012-03-31 17:45:29 +02:00
|
|
|
|
2012-04-26 20:34:49 +02:00
|
|
|
def on_async_poll(self, host, res, jid, clock):
|
2012-04-30 09:47:04 +02:00
|
|
|
if jid not in self._async_notified:
|
|
|
|
self._async_notified[jid] = clock + 1
|
|
|
|
if self._async_notified[jid] > clock:
|
|
|
|
self._async_notified[jid] = clock
|
|
|
|
print "<job %s> polling, %ss remaining"%(jid, clock)
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_async_poll(host, res, jid, clock)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_ok(self, host, res, jid):
|
2012-07-15 16:12:49 +02:00
|
|
|
print "<job %s> finished on %s => %s"%(jid, host, utils.jsonify(res,format=True))
|
2012-09-02 17:20:56 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_async_ok(host, res, jid)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_failed(self, host, res, jid):
|
2012-07-15 16:12:49 +02:00
|
|
|
print "<job %s> FAILED on %s => %s"%(jid, host, utils.jsonify(res,format=True))
|
2012-08-19 01:43:08 +02:00
|
|
|
super(CliRunnerCallbacks, self).on_async_failed(host,res,jid)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
2012-03-27 03:17:11 +02:00
|
|
|
def _on_any(self, host, result):
|
2012-08-21 02:41:28 +02:00
|
|
|
result2 = result.copy()
|
|
|
|
result2.pop('invocation', None)
|
2012-09-05 03:16:12 +02:00
|
|
|
print host_report_msg(host, self.options.module_name, result2, self.options.one_line)
|
2012-03-27 03:17:11 +02:00
|
|
|
if self.options.tree:
|
2012-09-05 03:16:12 +02:00
|
|
|
utils.write_tree_file(self.options.tree, host, utils.jsonify(result2,format=True))
|
2012-03-27 03:17:11 +02:00
|
|
|
|
2012-03-27 04:05:11 +02:00
|
|
|
########################################################################
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' callbacks used for Runner() from /usr/bin/ansible-playbook '''
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-08-09 03:09:14 +02:00
|
|
|
def __init__(self, stats, verbose=utils.VERBOSITY):
|
|
|
|
self.verbose = verbose
|
2012-03-26 01:05:27 +02:00
|
|
|
self.stats = stats
|
2012-04-30 09:47:04 +02:00
|
|
|
self._async_notified = {}
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-10-02 21:45:42 +02:00
|
|
|
def on_unreachable(self, host, results):
|
2012-07-20 14:56:33 +02:00
|
|
|
item = None
|
2012-10-02 21:45:42 +02:00
|
|
|
if type(results) == dict:
|
|
|
|
item = results.get('item', None)
|
2012-07-20 20:01:54 +02:00
|
|
|
if item:
|
2012-10-02 21:45:42 +02:00
|
|
|
msg = "fatal: [%s] => (item=%s) => %s" % (host, item, results)
|
2012-07-20 20:01:54 +02:00
|
|
|
else:
|
2012-10-02 21:45:42 +02:00
|
|
|
msg = "fatal: [%s] => %s" % (host, results)
|
|
|
|
print stringc(msg, 'red')
|
|
|
|
super(PlaybookRunnerCallbacks, self).on_unreachable(host, results)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-08-01 21:17:16 +02:00
|
|
|
def on_failed(self, host, results, ignore_errors=False):
|
2012-08-21 02:41:28 +02:00
|
|
|
|
2012-09-05 03:16:12 +02:00
|
|
|
results2 = results.copy()
|
|
|
|
results2.pop('invocation', None)
|
2012-08-21 02:41:28 +02:00
|
|
|
|
2012-09-05 03:16:12 +02:00
|
|
|
item = results2.get('item', None)
|
2012-09-26 02:36:13 +02:00
|
|
|
parsed = results2.get('parsed', True)
|
|
|
|
module_msg = ''
|
|
|
|
if not parsed:
|
|
|
|
module_msg = results2.pop('msg', None)
|
|
|
|
stderr = results2.pop('stderr', None)
|
|
|
|
stdout = results2.pop('stdout', None)
|
|
|
|
|
2012-07-20 20:01:54 +02:00
|
|
|
if item:
|
2012-09-05 03:16:12 +02:00
|
|
|
msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
|
2012-07-20 20:01:54 +02:00
|
|
|
else:
|
2012-09-05 03:16:12 +02:00
|
|
|
msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
|
2012-07-25 02:37:27 +02:00
|
|
|
print stringc(msg, 'red')
|
2012-09-26 02:36:13 +02:00
|
|
|
|
|
|
|
if stderr:
|
|
|
|
print stringc("stderr: %s" % stderr, 'red')
|
|
|
|
if stdout:
|
|
|
|
print stringc("stdout: %s" % stdout, 'red')
|
|
|
|
if not parsed and module_msg:
|
|
|
|
print stringc("invalid output was: %s" % module_msg, 'red')
|
2012-08-01 18:13:07 +02:00
|
|
|
if ignore_errors:
|
2012-10-02 21:49:04 +02:00
|
|
|
print stringc("...ignoring", 'cyan')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
|
|
|
def on_ok(self, host, host_result):
|
2012-07-20 20:01:54 +02:00
|
|
|
item = host_result.get('item', None)
|
|
|
|
|
2012-09-05 03:16:12 +02:00
|
|
|
host_result2 = host_result.copy()
|
|
|
|
host_result2.pop('invocation', None)
|
2012-09-25 14:06:55 +02:00
|
|
|
changed = host_result.get('changed', False)
|
2012-09-24 22:07:56 +02:00
|
|
|
ok_or_changed = 'ok'
|
|
|
|
if changed:
|
|
|
|
ok_or_changed = 'changed'
|
2012-08-21 02:41:28 +02:00
|
|
|
|
2012-06-20 03:55:57 +02:00
|
|
|
# show verbose output for non-setup module results if --verbose is used
|
2012-08-07 02:07:02 +02:00
|
|
|
msg = ''
|
2012-09-05 03:16:12 +02:00
|
|
|
if not self.verbose or host_result2.get("verbose_override",None) is not None:
|
2012-07-20 20:01:54 +02:00
|
|
|
if item:
|
2012-09-25 14:06:55 +02:00
|
|
|
msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item)
|
2012-08-07 02:07:02 +02:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
if 'ansible_job_id' not in host_result or 'finished' in host_result:
|
2012-09-24 22:07:56 +02:00
|
|
|
msg = "%s: [%s]" % (ok_or_changed, host)
|
2012-03-22 03:18:57 +01:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
# verbose ...
|
2012-07-20 20:01:54 +02:00
|
|
|
if item:
|
2012-09-24 22:07:56 +02:00
|
|
|
msg = "%s: [%s] => (item=%s) => %s" % (ok_or_changed, host, item, utils.jsonify(host_result2))
|
2012-07-20 20:01:54 +02:00
|
|
|
else:
|
2012-09-05 03:16:12 +02:00
|
|
|
if 'ansible_job_id' not in host_result or 'finished' in host_result2:
|
2012-09-25 14:06:55 +02:00
|
|
|
msg = "%s: [%s] => %s" % (ok_or_changed, host, utils.jsonify(host_result2))
|
2012-07-25 01:30:02 +02:00
|
|
|
|
|
|
|
if msg != '':
|
2012-09-24 22:07:56 +02:00
|
|
|
if not changed:
|
2012-07-25 01:30:02 +02:00
|
|
|
print stringc(msg, 'green')
|
|
|
|
else:
|
|
|
|
print stringc(msg, 'yellow')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
2012-03-30 22:17:16 +02:00
|
|
|
def on_error(self, host, err):
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-07-20 20:01:54 +02:00
|
|
|
item = err.get('item', None)
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = ''
|
2012-07-20 20:01:54 +02:00
|
|
|
if item:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "err: [%s] => (item=%s) => %s" % (host, item, err)
|
2012-07-20 20:01:54 +02:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "err: [%s] => %s" % (host, err)
|
|
|
|
|
|
|
|
msg = stringc(msg, 'red')
|
|
|
|
print >>sys.stderr, msg
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_error(host, err)
|
2012-03-30 22:17:16 +02:00
|
|
|
|
2012-07-24 01:36:26 +02:00
|
|
|
def on_skipped(self, host, item=None):
|
2012-08-02 23:25:45 +02:00
|
|
|
msg = ''
|
2012-07-25 01:30:02 +02:00
|
|
|
if item:
|
|
|
|
msg = "skipping: [%s] => (item=%s)" % (host, item)
|
2012-07-24 01:36:26 +02:00
|
|
|
else:
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "skipping: [%s]" % host
|
2012-10-02 21:49:04 +02:00
|
|
|
print stringc(msg, 'cyan')
|
2012-09-05 03:56:32 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_skipped(host, item)
|
2012-03-24 01:51:15 +01:00
|
|
|
|
2012-04-12 03:05:46 +02:00
|
|
|
def on_no_hosts(self):
|
2012-10-02 04:07:08 +02:00
|
|
|
print stringc("FATAL: no hosts matched or all hosts have already failed -- aborting\n", 'red')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_no_hosts()
|
2012-04-12 03:05:46 +02:00
|
|
|
|
2012-04-26 20:34:49 +02:00
|
|
|
def on_async_poll(self, host, res, jid, clock):
|
2012-04-30 09:47:04 +02:00
|
|
|
if jid not in self._async_notified:
|
|
|
|
self._async_notified[jid] = clock + 1
|
|
|
|
if self._async_notified[jid] > clock:
|
|
|
|
self._async_notified[jid] = clock
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "<job %s> polling, %ss remaining"%(jid, clock)
|
2012-07-26 02:29:20 +02:00
|
|
|
print stringc(msg, 'cyan')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_async_poll(host,res,jid,clock)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_ok(self, host, res, jid):
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "<job %s> finished on %s"%(jid, host)
|
2012-07-26 02:29:20 +02:00
|
|
|
print stringc(msg, 'cyan')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_async_ok(host, res, jid)
|
2012-04-26 20:34:49 +02:00
|
|
|
|
|
|
|
def on_async_failed(self, host, res, jid):
|
2012-07-25 01:30:02 +02:00
|
|
|
msg = "<job %s> FAILED on %s"%(jid, host)
|
|
|
|
print stringc(msg, 'red')
|
2012-08-19 01:43:08 +02:00
|
|
|
super(PlaybookRunnerCallbacks, self).on_async_failed(host,res,jid)
|
2012-07-25 01:30:02 +02:00
|
|
|
|
2012-04-26 20:34:49 +02:00
|
|
|
|
2012-03-27 04:05:11 +02:00
|
|
|
########################################################################
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
class PlaybookCallbacks(object):
|
2012-03-27 04:05:11 +02:00
|
|
|
''' playbook.py callbacks used by /usr/bin/ansible-playbook '''
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-06-20 03:55:57 +02:00
|
|
|
def __init__(self, verbose=False):
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-06-20 03:55:57 +02:00
|
|
|
self.verbose = verbose
|
2012-03-26 01:05:27 +02:00
|
|
|
|
|
|
|
def on_start(self):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_start')
|
2012-03-26 01:05:27 +02:00
|
|
|
|
|
|
|
def on_notify(self, host, handler):
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_notify', host, handler)
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-10-02 15:15:55 +02:00
|
|
|
def on_no_hosts_matched(self):
|
|
|
|
print stringc("no hosts matched", 'red')
|
|
|
|
call_callback_module('playbook_on_no_hosts_matched')
|
|
|
|
|
|
|
|
def on_no_hosts_remaining(self):
|
|
|
|
print stringc("\nFATAL: all hosts have already failed -- aborting", 'red')
|
|
|
|
call_callback_module('playbook_on_no_hosts_remaining')
|
|
|
|
|
2012-03-26 01:05:27 +02:00
|
|
|
def on_task_start(self, name, is_conditional):
|
2012-07-15 14:50:23 +02:00
|
|
|
msg = "TASK: [%s]" % name
|
|
|
|
if is_conditional:
|
2012-07-15 15:32:47 +02:00
|
|
|
msg = "NOTIFIED: [%s]" % name
|
2012-07-15 14:50:23 +02:00
|
|
|
print banner(msg)
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_task_start', name, is_conditional)
|
2012-03-26 01:05:27 +02:00
|
|
|
|
2012-08-09 16:56:40 +02:00
|
|
|
def on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None):
|
2012-07-15 18:29:53 +02:00
|
|
|
|
2012-07-24 16:43:29 +02:00
|
|
|
if prompt:
|
2012-10-02 04:15:00 +02:00
|
|
|
msg = "%s: " % prompt
|
2012-07-24 16:43:29 +02:00
|
|
|
else:
|
|
|
|
msg = 'input for %s: ' % varname
|
2012-08-09 16:56:40 +02:00
|
|
|
|
|
|
|
def prompt(prompt, private):
|
|
|
|
if private:
|
|
|
|
return getpass.getpass(prompt)
|
|
|
|
return raw_input(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
if confirm:
|
|
|
|
while True:
|
|
|
|
result = prompt(msg, private)
|
|
|
|
second = prompt("confirm " + msg, private)
|
2012-08-11 18:20:16 +02:00
|
|
|
if result == second:
|
|
|
|
break
|
2012-08-09 16:56:40 +02:00
|
|
|
print "***** VALUES ENTERED DO NOT MATCH ****"
|
|
|
|
else:
|
|
|
|
result = prompt(msg, private)
|
|
|
|
|
|
|
|
if encrypt:
|
|
|
|
result = utils.do_encrypt(result,encrypt,salt_size,salt)
|
|
|
|
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_vars_prompt', varname, private=private, prompt=prompt, encrypt=encrypt, confirm=confirm, salt_size=salt_size, salt=None)
|
|
|
|
|
2012-08-09 16:56:40 +02:00
|
|
|
return result
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-14 18:24:19 +02:00
|
|
|
def on_setup(self):
|
|
|
|
print banner("GATHERING FACTS")
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_setup')
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-03-22 03:18:57 +01:00
|
|
|
def on_import_for_host(self, host, imported_file):
|
2012-07-25 02:41:24 +02:00
|
|
|
msg = "%s: importing %s" % (host, imported_file)
|
2012-07-26 02:29:20 +02:00
|
|
|
print stringc(msg, 'cyan')
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_import_for_host', host, imported_file)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
|
|
|
def on_not_import_for_host(self, host, missing_file):
|
2012-07-25 02:41:24 +02:00
|
|
|
msg = "%s: not importing file: %s" % (host, missing_file)
|
2012-07-26 02:29:20 +02:00
|
|
|
print stringc(msg, 'cyan')
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_not_import_for_host', host, missing_file)
|
2012-03-22 03:18:57 +01:00
|
|
|
|
|
|
|
def on_play_start(self, pattern):
|
2012-05-26 17:52:01 +02:00
|
|
|
print banner("PLAY [%s]" % pattern)
|
2012-08-19 01:43:08 +02:00
|
|
|
call_callback_module('playbook_on_play_start', pattern)
|
|
|
|
|
|
|
|
def on_stats(self, stats):
|
|
|
|
call_callback_module('playbook_on_stats', stats)
|
2012-07-15 18:29:53 +02:00
|
|
|
|
|
|
|
|