Created
April 1, 2020 16:39
-
-
Save stobias123/cf55ac93430ef8edcfcbaf944ff894da to your computer and use it in GitHub Desktop.
Ansible Callback for wavefront.
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
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
import time | |
from wavefront_api_client.rest import ApiException | |
from pprint import pprint | |
from os.path import basename | |
import wavefront_api_client as wave_api | |
# not only visible to ansible-doc, it also 'declares' the options the plugin requires and how to configure them. | |
DOCUMENTATION = ''' | |
callback: wavefront | |
callback_type: aggregate | |
requirements: | |
- wavefront python modules. | |
short_description: Adds time to play stats | |
version_added: "2.9" | |
description: | |
- todo | |
''' | |
from datetime import datetime | |
from ansible.plugins.callback import CallbackBase | |
class CallbackModule(CallbackBase): | |
""" | |
This callback module sends events to wavefront at the end. | |
""" | |
CALLBACK_VERSION = 1.0 | |
CALLBACK_TYPE = 'aggregate' | |
CALLBACK_NAME = 'wavefront' | |
# only needed if you ship it and don't want to enable by default | |
CALLBACK_NEEDS_WHITELIST = True | |
def __init__(self): | |
# make sure the expected objects are present, calling the base's __init__ | |
super(CallbackModule, self).__init__() | |
self.base_url = 'wavefront_url' | |
self.api_key = 'api_key' | |
self.config = wave_api.Configuration() | |
self.config.host = self.base_url | |
self.client = wave_api.ApiClient(configuration=self.config, header_name='Authorization', header_value='Bearer ' + self.api_key) | |
self.api_instance = wave_api.EventApi(self.client) | |
self.event_body = wave_api.Event( | |
name="Ansible Playbook Run", | |
annotations={ | |
"severity": "info", | |
"type": "ansible-run", | |
"details": "ansible run" | |
}, | |
start_time=time.time() | |
) | |
def v2_playbook_on_start(self, playbook): | |
name = "Ansible Playbook Run: " % basename(playbook._file_name) | |
self.event_body.name = name | |
self.event_body.annotations['details'] = playbook._file_name | |
self.start_time = datetime.now().timestamp() | |
# this is only event we care about for display, when the play shows its summary stats; the rest are ignored by the base class | |
def v2_playbook_on_stats(self, stats): | |
self.event_body.end_time=time.time() | |
try: | |
# Create a specific event | |
api_response = self.api_instance.create_event(body=self.event_body) | |
except ApiException as e: | |
print("Exception when calling EventApi->create_event: %s\n" % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment