]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-cm-ansible.git/commitdiff
A callback plugin that logs failure message
authorAndrew Schoen <aschoen@redhat.com>
Mon, 3 Aug 2015 19:52:49 +0000 (14:52 -0500)
committerAndrew Schoen <aschoen@redhat.com>
Tue, 4 Aug 2015 19:27:51 +0000 (14:27 -0500)
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 <aschoen@redhat.com>
.gitignore
callback_plugins/failure_log.py [new file with mode: 0644]

index 0f3693cae0bf9c30ef12acbba7dd9c6119b07574..3a5b30d2d74fcb81154059b76b3321bedf6fd2a9 100644 (file)
@@ -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 (file)
index 0000000..d4ef8a1
--- /dev/null
@@ -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)