Skip to content

Instantly share code, notes, and snippets.

@MatthiasWinkelmann
Created May 22, 2016 17:58
Show Gist options
  • Save MatthiasWinkelmann/dcc6ea4f9edf88cee9996905dfb7184a to your computer and use it in GitHub Desktop.
Save MatthiasWinkelmann/dcc6ea4f9edf88cee9996905dfb7184a to your computer and use it in GitHub Desktop.
An Ansible plugin to format console output
# Save as <folder with your playbook>/callback_plugins/<some name>.py
# Optionally use no_log: True on your playbook/roles/tasks to suppress other output
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import time
import json
import sys
from ansible.utils.unicode import to_bytes
from ansible.plugins.callback import CallbackBase
import pprint
class CallbackModule(CallbackBase):
"""
logs playbook results, per host, in /var/log/ansible/hosts
"""
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
CALLBACK_NAME = 'log_plays'
TIME_FORMAT="%b %d %Y %H:%M:%S"
MSG_FORMAT="%(now)s - %(category)s - %(data)s\n\n"
def __init__(self):
super(CallbackModule, self).__init__()
def log(self, host, category, data):
pp = pprint.PrettyPrinter(indent=2, stream=sys.stdout)
pp.pprint(data)
def runner_on_failed(self, host, res, ignore_errors=False):
self.log(host, 'FAILED', res)
def runner_on_ok(self, host, res):
self.log(host, 'OK', res)
def runner_on_skipped(self, host, item=None):
self.log(host, 'SKIPPED', '...')
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment