From: Andrew Schoen Date: Mon, 3 Aug 2015 19:52:49 +0000 (-0500) Subject: A callback plugin that logs failure message X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1b8786bfd922c52e63c10153ecb6fb8d868b78f4;p=ceph-cm-ansible.git A callback plugin that logs failure message This callback plugin is called on every failure and prints out the failure information as yaml so that it's easier to read in the teuthology logs. If the environment variable ANSIBLE_FAILURE_LOG is present then a log of all failures in the playbook will be persisted to the file path given in the ANSIBLE_FAILURE_LOG. Signed-off-by: Andrew Schoen --- diff --git a/.gitignore b/.gitignore index 0f3693ca..3a5b30d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp virtualenv +*.pyc diff --git a/callback_plugins/failure_log.py b/callback_plugins/failure_log.py new file mode 100644 index 00000000..d4ef8a19 --- /dev/null +++ b/callback_plugins/failure_log.py @@ -0,0 +1,56 @@ +import yaml +import os +import logging + +log = logging.getLogger(__name__) + + +def log_failure(host, result): + """ + Print any failures to stdout nicely formatted as yaml. + + If the environment variable ANSIBLE_FAILURE_LOG is present + a log of all failures in the playbook will be persisted to + the file path given in ANSIBLE_FAILURE_LOG. + """ + failure = {"{0}".format(host): dict()} + failure[host] = result + + print "*******\n" + print yaml.safe_dump(failure) + + fail_log = os.environ.get('ANSIBLE_FAILURE_LOG') + if fail_log: + existing_failures = dict() + if os.path.exists(fail_log): + with open(fail_log, 'r') as outfile: + existing_failures = yaml.safe_load(outfile.read()) + + if existing_failures: + failure.update(existing_failures) + + with open(fail_log, 'w') as outfile: + outfile.write(yaml.safe_dump(failure)) + + +class CallbackModule(object): + """ + This Ansible callback plugin writes task failures to a yaml file. + """ + + def runner_on_failed(self, host, result, ignore_errors=False): + """ + A hook that will be called on every task failure. + """ + + if ignore_errors: + return + + log_failure(host, result) + + def runner_on_unreachable(self, host, result): + """ + A hook that will be called on every task that is unreachable. + """ + + log_failure(host, result)