| 1 |
# Copyright (C) 2016 maxn [email protected] |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
author: Unknown (!UNKNOWN) |
| 10 |
name: jabber |
| 11 |
type: notification |
| 12 |
short_description: Post task events to a Jabber server |
| 13 |
description: |
| 14 |
- The chatty part of ChatOps with a Hipchat server as a target. |
| 15 |
- This callback plugin sends status updates to a HipChat channel during playbook execution. |
| 16 |
requirements: |
| 17 |
- xmpp (Python library U(https://github.com/ArchipelProject/xmpppy)) |
| 18 |
options: |
| 19 |
server: |
| 20 |
description: Connection info to Jabber server. |
| 21 |
type: str |
| 22 |
required: true |
| 23 |
env: |
| 24 |
- name: JABBER_SERV |
| 25 |
user: |
| 26 |
description: Jabber user to authenticate as. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
env: |
| 30 |
- name: JABBER_USER |
| 31 |
password: |
| 32 |
description: Password for the user to the Jabber server. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
env: |
| 36 |
- name: JABBER_PASS |
| 37 |
to: |
| 38 |
description: Chat identifier that receives the message. |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
env: |
| 42 |
- name: JABBER_TO |
| 43 |
""" |
| 44 |
|
| 45 |
import os |
| 46 |
|
| 47 |
HAS_XMPP = True |
| 48 |
try: |
| 49 |
import xmpp |
| 50 |
except ImportError: |
| 51 |
HAS_XMPP = False |
| 52 |
|
| 53 |
from ansible.plugins.callback import CallbackBase |
| 54 |
|
| 55 |
|
| 56 |
class CallbackModule(CallbackBase): |
| 57 |
CALLBACK_VERSION = 2.0 |
| 58 |
CALLBACK_TYPE = "notification" |
| 59 |
CALLBACK_NAME = "community.general.jabber" |
| 60 |
CALLBACK_NEEDS_WHITELIST = True |
| 61 |
|
| 62 |
def __init__(self, display=None): |
| 63 |
super().__init__(display=display) |
| 64 |
|
| 65 |
if not HAS_XMPP: |
| 66 |
self._display.warning( |
| 67 |
"The required python xmpp library (xmpppy) is not installed. " |
| 68 |
"pip install git+https://github.com/ArchipelProject/xmpppy" |
| 69 |
) |
| 70 |
self.disabled = True |
| 71 |
|
| 72 |
self.serv = os.getenv("JABBER_SERV") |
| 73 |
self.j_user = os.getenv("JABBER_USER") |
| 74 |
self.j_pass = os.getenv("JABBER_PASS") |
| 75 |
self.j_to = os.getenv("JABBER_TO") |
| 76 |
|
| 77 |
if (self.j_user or self.j_pass or self.serv or self.j_to) is None: |
| 78 |
self.disabled = True |
| 79 |
self._display.warning( |
| 80 |
"Jabber CallBack wants the JABBER_SERV, JABBER_USER, JABBER_PASS and JABBER_TO environment variables" |
| 81 |
) |
| 82 |
|
| 83 |
def send_msg(self, msg): |
| 84 |
"""Send message""" |
| 85 |
jid = xmpp.JID(self.j_user) |
| 86 |
client = xmpp.Client(self.serv, debug=[]) |
| 87 |
client.connect(server=(self.serv, 5222)) |
| 88 |
client.auth(jid.getNode(), self.j_pass, resource=jid.getResource()) |
| 89 |
message = xmpp.Message(self.j_to, msg) |
| 90 |
message.setAttr("type", "chat") |
| 91 |
client.send(message) |
| 92 |
client.disconnect() |
| 93 |
|
| 94 |
def v2_runner_on_ok(self, result): |
| 95 |
self._clean_results(result._result, result._task.action) |
| 96 |
self.debug = self._dump_results(result._result) |
| 97 |
|
| 98 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 99 |
self.task = task |
| 100 |
|
| 101 |
def v2_playbook_on_play_start(self, play): |
| 102 |
"""Display Playbook and play start messages""" |
| 103 |
self.play = play |
| 104 |
name = play.name |
| 105 |
self.send_msg(f"Ansible starting play: {name}") |
| 106 |
|
| 107 |
def playbook_on_stats(self, stats): |
| 108 |
name = self.play |
| 109 |
hosts = sorted(stats.processed.keys()) |
| 110 |
failures = False |
| 111 |
unreachable = False |
| 112 |
for h in hosts: |
| 113 |
s = stats.summarize(h) |
| 114 |
if s["failures"] > 0: |
| 115 |
failures = True |
| 116 |
if s["unreachable"] > 0: |
| 117 |
unreachable = True |
| 118 |
|
| 119 |
if failures or unreachable: |
| 120 |
out = self.debug |
| 121 |
self.send_msg(f"{name}: Failures detected \n{self.task} \nHost: {h}\n Failed at:\n{out}") |
| 122 |
else: |
| 123 |
out = self.debug |
| 124 |
self.send_msg(f"Great! \n Playbook {name} completed:\n{s} \n Last task debug:\n {out}") |