From 136ebfd00177319f77240e72aa0b4b22841c9514 Mon Sep 17 00:00:00 2001 From: Chris Hammer Date: Fri, 23 Jun 2023 17:08:03 -0400 Subject: [PATCH] add initial callback plugin poc draft; add simple debug.yml play to test easier --- .gitignore | 1 + callback_plugins/webhook_callback.py | 139 +++++++++++++++++++++++++++ debug.yml | 11 +++ test.py | 24 +++++ 4 files changed, 175 insertions(+) create mode 100644 callback_plugins/webhook_callback.py create mode 100644 debug.yml create mode 100644 test.py diff --git a/.gitignore b/.gitignore index 3a156f6..9c8d489 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ collections facts.d .vscode +callback_plugins/__pycache__ diff --git a/callback_plugins/webhook_callback.py b/callback_plugins/webhook_callback.py new file mode 100644 index 0000000..96a0e8c --- /dev/null +++ b/callback_plugins/webhook_callback.py @@ -0,0 +1,139 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +from ansible.plugins.callback import CallbackBase +from ansible import constants as C +from ansible.utils.color import colorize, hostcolor +from ansible.utils.display import Display + + +class CallbackModule(CallbackBase): + CALLBACK_VERSION = 2.0 + CALLBACK_TYPE = 'notification' + CALLBACK_NAME = 'hello_bob' + + def __init__(self, *args, **kwargs): + super(CallbackModule, self).__init__() + + + def v2_playbook_on_start(self, playbook): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_task_start(self, task, is_conditional): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_stats(self, stats): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + +################################################################################################################### + + def v2_on_any(self, *args, **kwargs): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_failed(self, result, ignore_errors=False): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_ok(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_skipped(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_unreachable(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_async_poll(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_async_ok(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_async_failed(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_start(self, playbook): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_notify(self, handler, host): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_no_hosts_matched(self): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_no_hosts_remaining(self): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_task_start(self, task, is_conditional): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_cleanup_task_start(self, task): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_handler_task_start(self, task): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None, unsafe=None): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_import_for_host(self, result, imported_file): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_not_import_for_host(self, result, missing_file): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_play_start(self, play): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_stats(self, stats): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_on_file_diff(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_playbook_on_include(self, included_file): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_item_on_ok(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_item_on_failed(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_item_on_skipped(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_retry(self, result): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + + + def v2_runner_on_start(self, host, task): + self._display.display("Custom commands for go here", color=C.COLOR_OK) + diff --git a/debug.yml b/debug.yml new file mode 100644 index 0000000..2677b6c --- /dev/null +++ b/debug.yml @@ -0,0 +1,11 @@ +- name: "Simple Test" + hosts: localhost + connection: local + gather_facts: false + become: false + + + tasks: + - name: Say hello, Bob + ansible.builtin.debug: + msg: Hi. diff --git a/test.py b/test.py new file mode 100644 index 0000000..1e028d5 --- /dev/null +++ b/test.py @@ -0,0 +1,24 @@ +import requests,urllib.error, json, re + + +################# +# Webhook Vars: # +################# +#webhook_endpoint = "https://webhooks.thezengarden.net/web/webhook-callback-poc" +#webhook_token = "5S2OtSb9KXWDyV8xYl4_Rvucz2KZHyFDRkFbRzLj78yP" +webhook_endpoint = "https://webhooks.thezengarden.net/web/webhook-callback-poc?token=5S2OtSb9KXWDyV8xYl4_Rvucz2KZHyFDRkFbRzLj78yP" + + +print("sending webhook payload...") + +# webhook_auth_header = {'Token': webhook_token } +webhook_payload = {'webhook-callback-poc': "This is a sample message sent to a webhook."} +resp = requests.post(webhook_endpoint, + headers=webhook_auth_header, + json = webhook_payload) + +print("payload sent!") +print(resp) +print(webhook_endpoint) +print(webhook_auth_header) +print(webhook_payload)