log_parse 1.91 KB
Newer Older
1 2 3 4
/bin/cat << EOF > $ANSIBLE_PLUGIN_LOCATION/log_parse.py
import os
import time
import json
5
from ansible.plugins.callback import CallbackBase
6

7
class CallbackModule(CallbackBase):
8 9 10 11 12 13 14 15
    """
    logs playbook results, per host, in /var/log/ansible/hosts
    """

    log_path = '/var/log/ansible/hosts'

    def __init__(self):

16 17
        super(CallbackModule, self).__init__()

18 19 20 21 22 23 24 25 26
        if not os.path.exists(self.log_path):
            os.makedirs(self.log_path)
        else:
          for filename in os.listdir(self.log_path):
            filepath = os.path.join(self.log_path, filename)
            if os.path.exists(filepath) and os.path.isfile(filepath):
              os.unlink(filepath)

    def log(self, host, category, data, ignore_errors=False):
27

28 29 30 31 32 33 34 35 36
        if type(data) == dict:
            if '_ansible_verbose_override' in data:
                # avoid logging extraneous data
                data = {}
            else:
                data = data.copy()
                data = json.dumps(data)

            if ignore_errors:
37
              category = '%s_IGNORED' % category
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            path = os.path.join("/var/log/ansible/hosts", '%s_%s' % (host, category))
            with open(path, "a") as fd:
              fd.write(data)
              fd.write('\n')

    def runner_on_failed(self, host, res, ignore_errors=False):
        self.log(host, 'FAILED', res, ignore_errors)

    def runner_on_ok(self, host, res):
        self.log(host, 'OK', res)

    def runner_on_skipped(self, host, item=None):
        pass

    def runner_on_unreachable(self, host, res):
        self.log(host, 'UNREACHABLE', res)

    def runner_on_async_failed(self, host, res, jid):
        self.log(host, 'ASYNC_FAILED', res)

    def playbook_on_import_for_host(self, host, imported_file):
        self.log(host, 'IMPORTED', imported_file)

    def playbook_on_not_import_for_host(self, host, missing_file):
        self.log(host, 'NOTIMPORTED', missing_file)

EOF