Skip to content
Snippets Groups Projects
Commit 4c1872aa authored by jurgenhaas's avatar jurgenhaas
Browse files

ansible/playbooks/general#104 Add project ID for Ansible alerts and add the...

ansible/playbooks/general#104 Add project ID for Ansible alerts and add the full result as json dump
parent 34ed87a8
Branches
Tags
No related merge requests found
......@@ -3,6 +3,7 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import logging
import os
import requests
......@@ -20,9 +21,10 @@ class CallbackModule(CallbackBase):
and put the plugin in <path_to_callback_plugins_folder>
This plugin makes use of the following required environment variables:
ALERTA_COMPANY : The inventory
ALERTA_WEBHOOK : The Alerta webhook
ALERTA_APIKEY : The Alerta API key
ALERTA_COMPANY : The inventory
ALERTA_WEBHOOK : The Alerta webhook
ALERTA_APIKEY : The Alerta API key
ALERTA_GITLAB_PID : The project ID in GitLab
"""
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'aggregate'
......@@ -39,10 +41,11 @@ class CallbackModule(CallbackBase):
self.logger.setLevel(logging.FATAL)
self.hostname = socket.gethostname()
self.company = os.getenv('ALERTA_COMPANY', '')
self.pid = os.getenv('ALERTA_GITLAB_PID', '')
self.session = str(uuid.uuid1())
self.playbook = ''
def _log(self, severity, task=None):
def _log(self, severity, task=None, result=None):
if self.logger.isEnabledFor(severity):
values = {
'host': self.hostname,
......@@ -57,6 +60,10 @@ class CallbackModule(CallbackBase):
'value': '',
'rawData': '',
}
if self.pid != '':
values['attributes'] = {"gitlab_project_id": "%s" % self.pid}
if result is not None:
values['rawData'] = json.dumps(result)
headers = {
'Authorization': 'Key %s' % os.getenv('ALERTA_APIKEY', ''),
'Content-type': 'application/json'
......@@ -78,14 +85,14 @@ class CallbackModule(CallbackBase):
severity = logging.INFO
else:
severity = logging.DEBUG
self._log(severity, task=task_name)
self._log(severity, task=task_name, result=result)
def v2_runner_on_failed(self, result, **kwargs):
result._result['failed'] = True
self.v2_runner_on_ok(result)
def v2_runner_on_unreachable(self, result):
self._log(logging.FATAL)
self._log(logging.FATAL, result=result)
def v2_runner_on_async_ok(self, result):
self.v2_runner_on_ok(result)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment