2015-02-10 15:43:17 +01:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
|
2015-07-07 12:49:06 +02:00
|
|
|
import socket
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
class CallbackModule(object):
|
|
|
|
"""
|
|
|
|
logs ansible-playbook and ansible runs to a syslog server in json format
|
|
|
|
make sure you have in ansible.cfg:
|
|
|
|
callback_plugins = <path_to_callback_plugins_folder>
|
|
|
|
and put the plugin in <path_to_callback_plugins_folder>
|
|
|
|
|
|
|
|
This plugin makes use of the following environment variables:
|
|
|
|
SYSLOG_SERVER (optional): defaults to localhost
|
|
|
|
SYSLOG_PORT (optional): defaults to 514
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.logger = logging.getLogger('ansible logger')
|
|
|
|
self.logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
self.handler = logging.handlers.SysLogHandler(
|
2015-06-16 16:13:01 +02:00
|
|
|
address = (os.getenv('SYSLOG_SERVER','localhost'),
|
2015-02-10 15:43:17 +01:00
|
|
|
os.getenv('SYSLOG_PORT',514)),
|
|
|
|
facility=logging.handlers.SysLogHandler.LOG_USER
|
|
|
|
)
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.addHandler(self.handler)
|
|
|
|
self.hostname = socket.gethostname()
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def on_any(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def runner_on_failed(self, host, res, ignore_errors=False):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def runner_on_ok(self, host, res):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.info('%s ansible-command: task execution OK; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def runner_on_skipped(self, host, item=None):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.info('%s ansible-command: task execution SKIPPED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def runner_on_unreachable(self, host, res):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.error('%s ansible-command: task execution UNREACHABLE; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def runner_on_no_hosts(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def runner_on_async_poll(self, host, res):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def runner_on_async_ok(self, host, res):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def runner_on_async_failed(self, host, res):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
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):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.info('%s ansible-command: playbook IMPORTED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def playbook_on_not_import_for_host(self, host, missing_file):
|
2015-07-07 12:49:06 +02:00
|
|
|
self.logger.info('%s ansible-command: playbook NOT IMPORTED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True)))
|
2015-02-10 15:43:17 +01:00
|
|
|
|
|
|
|
def playbook_on_play_start(self, name):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def playbook_on_stats(self, stats):
|
|
|
|
pass
|