]> git.apps.os.sepia.ceph.com Git - ceph-cm-ansible.git/commitdiff
A callback plugin that logs failure messages 96/head
authorAndrew Schoen <aschoen@redhat.com>
Mon, 3 Aug 2015 19:52:49 +0000 (14:52 -0500)
committerAndrew Schoen <aschoen@redhat.com>
Wed, 5 Aug 2015 17:25:37 +0000 (12:25 -0500)
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..78ddbfc
--- /dev/null
@@ -0,0 +1,57 @@
+"""
+This callback plugin writes ansible failures to a log as yaml. This way you
+can parse the file later and use the ansible failures for other reporting
+or logging.
+
+A log will not be written unless the environment variable ANSIBLE_FAILURE_LOG
+is present and contains a path to a file to write the log to.
+
+NOTE: This plugin was only tested on ansible version 1.9.2 and does not support
+v2 style plugins
+"""
+import yaml
+import os
+import logging
+
+log = logging.getLogger(__name__)
+# We only want to log if this env var is populated with
+# a file path of where the log should live.
+fail_log = os.environ.get('ANSIBLE_FAILURE_LOG')
+if fail_log:
+    handler = logging.FileHandler(filename=fail_log)
+    log.addHandler(handler)
+
+
+def log_failure(host, result):
+    """
+    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.
+    """
+    if fail_log:
+        failure = {"{0}".format(host): dict()}
+        failure[host] = result
+        log.error(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)