| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Deepak Kothandan <[email protected]> |
| 4 |
# Copyright (c) 2015, Stefan Berggren <[email protected]> |
| 5 |
# Copyright (c) 2014, Ramon de la Fuente <[email protected]> |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: rocketchat |
| 14 |
short_description: Send notifications to Rocket Chat |
| 15 |
description: |
| 16 |
- This module sends notifications to Rocket Chat through the Incoming WebHook integration. |
| 17 |
author: "Ramon de la Fuente (@ramondelafuente)" |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: none |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
domain: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- The domain for your environment without protocol. (For example V(example.com) or V(chat.example.com)). |
| 30 |
required: true |
| 31 |
token: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Rocket Chat Incoming Webhook integration token. This provides authentication to Rocket Chat's Incoming webhook for |
| 35 |
posting messages. |
| 36 |
required: true |
| 37 |
protocol: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- Specify the protocol used to send notification messages before the webhook URL (that is, V(http) or V(https)). |
| 41 |
default: https |
| 42 |
choices: |
| 43 |
- 'http' |
| 44 |
- 'https' |
| 45 |
msg: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- Message to be sent. |
| 49 |
channel: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- Channel to send the message to. If absent, the message goes to the channel selected for the O(token) specified during |
| 53 |
the creation of webhook. |
| 54 |
username: |
| 55 |
type: str |
| 56 |
description: |
| 57 |
- This is the sender of the message. |
| 58 |
default: "Ansible" |
| 59 |
icon_url: |
| 60 |
type: str |
| 61 |
description: |
| 62 |
- URL for the message sender's icon. |
| 63 |
default: "https://docs.ansible.com/favicon/favicon.ico" |
| 64 |
icon_emoji: |
| 65 |
type: str |
| 66 |
description: |
| 67 |
- Emoji for the message sender. The representation for the available emojis can be got from Rocket Chat. |
| 68 |
- For example V(:thumbsup:). |
| 69 |
- If O(icon_emoji) is set, O(icon_url) is not used. |
| 70 |
link_names: |
| 71 |
type: int |
| 72 |
description: |
| 73 |
- Automatically create links for channels and usernames in O(msg). |
| 74 |
default: 1 |
| 75 |
choices: |
| 76 |
- 1 |
| 77 |
- 0 |
| 78 |
validate_certs: |
| 79 |
description: |
| 80 |
- If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed |
| 81 |
certificates. |
| 82 |
type: bool |
| 83 |
default: true |
| 84 |
color: |
| 85 |
type: str |
| 86 |
description: |
| 87 |
- Allow text to use default colors - use the default of V(normal) to not send a custom color bar at the start of the |
| 88 |
message. |
| 89 |
default: 'normal' |
| 90 |
choices: |
| 91 |
- 'normal' |
| 92 |
- 'good' |
| 93 |
- 'warning' |
| 94 |
- 'danger' |
| 95 |
attachments: |
| 96 |
type: list |
| 97 |
elements: dict |
| 98 |
description: |
| 99 |
- Define a list of attachments. |
| 100 |
is_pre740: |
| 101 |
description: |
| 102 |
- If V(true), the payload matches Rocket.Chat prior to 7.4.0 format. This format has been used by the module since its |
| 103 |
inception, but is no longer supported by Rocket.Chat 7.4.0. |
| 104 |
- The default value changed from V(true) to V(false) in community.general 13.0.0. |
| 105 |
- This parameter is going to be removed in a future release when Rocket.Chat 7.4.0 becomes the minimum supported version. |
| 106 |
type: bool |
| 107 |
default: false |
| 108 |
version_added: 10.5.0 |
| 109 |
""" |
| 110 |
|
| 111 |
EXAMPLES = r""" |
| 112 |
- name: Send notification message through Rocket Chat |
| 113 |
community.general.rocketchat: |
| 114 |
token: thetoken/generatedby/rocketchat |
| 115 |
domain: chat.example.com |
| 116 |
msg: '{{ inventory_hostname }} completed' |
| 117 |
delegate_to: localhost |
| 118 |
|
| 119 |
- name: Send notification message through Rocket Chat all options |
| 120 |
community.general.rocketchat: |
| 121 |
domain: chat.example.com |
| 122 |
token: thetoken/generatedby/rocketchat |
| 123 |
msg: '{{ inventory_hostname }} completed' |
| 124 |
channel: "#ansible" |
| 125 |
username: 'Ansible on {{ inventory_hostname }}' |
| 126 |
icon_url: http://www.example.com/some-image-file.png |
| 127 |
link_names: 0 |
| 128 |
delegate_to: localhost |
| 129 |
|
| 130 |
- name: Insert a color bar in front of the message for visibility purposes and use the default webhook icon and name configured |
| 131 |
in rocketchat |
| 132 |
community.general.rocketchat: |
| 133 |
token: thetoken/generatedby/rocketchat |
| 134 |
domain: chat.example.com |
| 135 |
msg: '{{ inventory_hostname }} is alive!' |
| 136 |
color: good |
| 137 |
username: '' |
| 138 |
icon_url: '' |
| 139 |
delegate_to: localhost |
| 140 |
|
| 141 |
- name: Use the attachments API |
| 142 |
community.general.rocketchat: |
| 143 |
token: thetoken/generatedby/rocketchat |
| 144 |
domain: chat.example.com |
| 145 |
attachments: |
| 146 |
- text: Display my system load on host A and B |
| 147 |
color: "#ff00dd" |
| 148 |
title: System load |
| 149 |
fields: |
| 150 |
- title: System A |
| 151 |
value: 'load average: 0,74, 0,66, 0,63' |
| 152 |
short: true |
| 153 |
- title: System B |
| 154 |
value: 'load average: 5,16, 4,64, 2,43' |
| 155 |
short: true |
| 156 |
delegate_to: localhost |
| 157 |
""" |
| 158 |
|
| 159 |
|
| 160 |
from ansible.module_utils.basic import AnsibleModule |
| 161 |
from ansible.module_utils.urls import fetch_url |
| 162 |
|
| 163 |
|
| 164 |
def build_payload_for_rocketchat( |
| 165 |
module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740 |
| 166 |
): |
| 167 |
payload = {} |
| 168 |
if color == "normal" and text is not None: |
| 169 |
payload = dict(text=text) |
| 170 |
elif text is not None: |
| 171 |
payload = dict(attachments=[dict(text=text, color=color)]) |
| 172 |
if channel is not None: |
| 173 |
if channel[0] == "#" or channel[0] == "@": |
| 174 |
payload["channel"] = channel |
| 175 |
else: |
| 176 |
payload["channel"] = f"#{channel}" |
| 177 |
if username is not None: |
| 178 |
payload["username"] = username |
| 179 |
if icon_emoji is not None: |
| 180 |
payload["icon_emoji"] = icon_emoji |
| 181 |
else: |
| 182 |
payload["icon_url"] = icon_url |
| 183 |
if link_names is not None: |
| 184 |
payload["link_names"] = link_names |
| 185 |
|
| 186 |
if attachments is not None: |
| 187 |
if "attachments" not in payload: |
| 188 |
payload["attachments"] = [] |
| 189 |
|
| 190 |
if attachments is not None: |
| 191 |
for attachment in attachments: |
| 192 |
if "fallback" not in attachment: |
| 193 |
attachment["fallback"] = attachment["text"] |
| 194 |
payload["attachments"].append(attachment) |
| 195 |
|
| 196 |
payload = module.jsonify(payload) |
| 197 |
if is_pre740: |
| 198 |
payload = f"payload={payload}" |
| 199 |
return payload |
| 200 |
|
| 201 |
|
| 202 |
def do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740): |
| 203 |
if token.count("/") < 1: |
| 204 |
module.fail_json(msg="Invalid Token specified, provide a valid token") |
| 205 |
|
| 206 |
rocketchat_incoming_webhook = f"{protocol}://{domain}/hooks/{token}" |
| 207 |
|
| 208 |
headers = None |
| 209 |
if not is_pre740: |
| 210 |
headers = {"Content-type": "application/json"} |
| 211 |
response, info = fetch_url(module, rocketchat_incoming_webhook, data=payload, headers=headers) |
| 212 |
if info["status"] != 200: |
| 213 |
module.fail_json(msg=f"failed to send message, return status={info['status']}") |
| 214 |
|
| 215 |
|
| 216 |
def main(): |
| 217 |
module = AnsibleModule( |
| 218 |
argument_spec=dict( |
| 219 |
domain=dict(type="str", required=True), |
| 220 |
token=dict(type="str", required=True, no_log=True), |
| 221 |
protocol=dict(type="str", default="https", choices=["http", "https"]), |
| 222 |
msg=dict(type="str"), |
| 223 |
channel=dict(type="str"), |
| 224 |
username=dict(type="str", default="Ansible"), |
| 225 |
icon_url=dict(type="str", default="https://docs.ansible.com/favicon/favicon.ico"), |
| 226 |
icon_emoji=dict(type="str"), |
| 227 |
link_names=dict(type="int", default=1, choices=[0, 1]), |
| 228 |
validate_certs=dict(default=True, type="bool"), |
| 229 |
color=dict(type="str", default="normal", choices=["normal", "good", "warning", "danger"]), |
| 230 |
attachments=dict(type="list", elements="dict"), |
| 231 |
is_pre740=dict(type="bool", default=False), |
| 232 |
) |
| 233 |
) |
| 234 |
|
| 235 |
domain = module.params["domain"] |
| 236 |
token = module.params["token"] |
| 237 |
protocol = module.params["protocol"] |
| 238 |
text = module.params["msg"] |
| 239 |
channel = module.params["channel"] |
| 240 |
username = module.params["username"] |
| 241 |
icon_url = module.params["icon_url"] |
| 242 |
icon_emoji = module.params["icon_emoji"] |
| 243 |
link_names = module.params["link_names"] |
| 244 |
color = module.params["color"] |
| 245 |
attachments = module.params["attachments"] |
| 246 |
is_pre740 = module.params["is_pre740"] |
| 247 |
|
| 248 |
payload = build_payload_for_rocketchat( |
| 249 |
module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740 |
| 250 |
) |
| 251 |
do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740) |
| 252 |
|
| 253 |
module.exit_json(msg="OK") |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == "__main__": |
| 257 |
main() |