Created
March 26, 2015 00:15
-
-
Save inokappa/7b73c5e7c0ab9087841d to your computer and use it in GitHub Desktop.
Anaible の各 task を fluentd でロギングする Callback module もどき
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import json | |
from fluent import sender | |
from fluent import event | |
TIME_FORMAT="%b %d %Y %H:%M:%S" | |
MSG_FORMAT="%(now)s - %(category)s - %(data)s\n\n" | |
def log(host, category, data): | |
if type(data) == dict: | |
if 'verbose_override' in data: | |
# avoid logging extraneous data from facts | |
data = 'omitted' | |
else: | |
data = data.copy() | |
invocation = data.pop('invocation', None) | |
data = json.dumps(data) | |
if invocation is not None: | |
data = json.dumps(invocation) + " => %s " % data | |
now = time.strftime(TIME_FORMAT, time.localtime()) | |
sender.setup('ansible', host='localhost', port=24224) | |
event.Event(host, { | |
'host': host, | |
'category': category, | |
'messages': data | |
}) | |
class CallbackModule(object): | |
""" | |
logs playbook results, per host, in /var/log/ansible/hosts | |
""" | |
def on_any(self, *args, **kwargs): | |
pass | |
def runner_on_failed(self, host, res, ignore_errors=False): | |
log(host, 'FAILED', res) | |
def runner_on_ok(self, host, res): | |
log(host, 'OK', res) | |
def runner_on_skipped(self, host, item=None): | |
log(host, 'SKIPPED', '...') | |
def runner_on_unreachable(self, host, res): | |
log(host, 'UNREACHABLE', res) | |
def runner_on_no_hosts(self): | |
pass | |
def runner_on_async_poll(self, host, res, jid, clock): | |
pass | |
def runner_on_async_ok(self, host, res, jid): | |
pass | |
def runner_on_async_failed(self, host, res, jid): | |
log(host, 'ASYNC_FAILED', res) | |
def playbook_on_start(self): | |
pass | |
def playbook_on_notify(self, host, handler): | |
pass | |
def playbook_on_no_hosts_matched(self): | |
pass | |
def playbook_on_no_hosts_remaining(self): | |
pass | |
def playbook_on_task_start(self, name, is_conditional): | |
pass | |
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None): | |
pass | |
def playbook_on_setup(self): | |
pass | |
def playbook_on_import_for_host(self, host, imported_file): | |
log(host, 'IMPORTED', imported_file) | |
def playbook_on_not_import_for_host(self, host, missing_file): | |
log(host, 'NOTIMPORTED', missing_file) | |
def playbook_on_play_start(self, name): | |
pass | |
def playbook_on_stats(self, stats): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment