Last active
July 10, 2018 07:38
-
-
Save odyssey4me/72872b19b3c5ec0063baf6b0b3a0361a to your computer and use it in GitHub Desktop.
Ansible 2.3 callback to record events in InfluxDB
This file contains hidden or 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
# place in ~/.ansible.cfg | |
[defaults] | |
callback_whitelist = influxdb_events | |
retry_files_enabled = False |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# Make coding more python3-ish | |
# place in ~/.ansible/plugins/callback/influxdb_events.py | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
import os | |
from datetime import datetime | |
from influxdb import InfluxDBClient | |
from influxdb.client import InfluxDBClientError | |
try: | |
from __main__ import cli | |
except ImportError: | |
cli = None | |
from ansible.plugins.callback import CallbackBase | |
class CallbackModule(CallbackBase): | |
""" | |
This callback module fills an InfluxDB with Ansible events with the tag 'log'. | |
""" | |
CALLBACK_VERSION = 2.0 | |
CALLBACK_TYPE = 'notification' | |
CALLBACK_NAME = 'influxdb_events' | |
CALLBACK_NEEDS_WHITELIST = True | |
def __init__(self): | |
super(CallbackModule, self).__init__() | |
self.region = None | |
self.events = [] | |
if cli: | |
self._options = cli.options | |
else: | |
self._options = None | |
def report_event(self, fields, status): | |
"""Add a new event in the list""" | |
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f') | |
event = { | |
'time': current_time, | |
'measurement': 'events', | |
'fields': fields, | |
'tags': { 'status': status } | |
} | |
self.events.append(event) | |
def v2_playbook_on_start(self, playbook): | |
extra_vars = self._options.extra_vars | |
if extra_vars: | |
for ev in extra_vars: | |
key, value = ev.split('=') | |
if key == 'region': | |
self.region = value | |
def v2_runner_on_failed(self, result, ignore_errors=False): | |
"""Log each failed task""" | |
if not result._task.tags: | |
return | |
elif result._task.tags[0] != "log": | |
return | |
fields = { | |
'ansible_task': str(result._task), | |
'region': str(self.region) | |
} | |
self.report_event(fields, "FAILED") | |
def v2_runner_on_ok(self, result): | |
"""Log each successful task""" | |
if not result._task.tags: | |
return | |
elif result._task.tags[0] != "log": | |
return | |
fields = { | |
'ansible_task': str(result._task), | |
'region': str(self.region) | |
} | |
self.report_event(fields, "OK") | |
def v2_playbook_on_stats(self, stats): | |
"""Connect to InfluxDB and commit events""" | |
_host = "localhost" | |
_port = "8086" | |
_user = "None" | |
_pass = "None" | |
_dbname = "events" | |
influxdb = InfluxDBClient(_host, _port, _user, _pass, _dbname) | |
try: | |
influxdb.write_points(self.events, time_precision='u') | |
except Exception: | |
# Disable the plugin if writes fail | |
self.disabled = True | |
self._display.warning( | |
"Cannot write to InfluxDB, check the service state " | |
"on %s." % _host) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment