added color bar option to Slack module

This update will allow people to add a color bar at the front of a
Slack notification using the default 3 colors by name Slack specify
(good, warning, danger).

If no color is specified, or the default is used (normal) then no bar
will be added.

Description and example also added in this update.

Color bars are added by using the attachments json object inside the
payload - this is a very simplistic implementation as using custom
colors or adding titles or other formatting are not included in this
update and if needed I’m sure somebody else can spend the time to add
them later…

Tested with ansible 1.7
pull/18777/head
Phillip Holmes 10 years ago committed by Matt Clay
parent 3e5a7bd475
commit 66aa605367

@ -89,6 +89,16 @@ options:
choices: choices:
- 'yes' - 'yes'
- 'no' - 'no'
color:
description:
- Allow text to use default colors - use the default of 'normal' to not send a custom color bar at the start of the message
required: false
default: 'normal'
choices:
- 'normal'
- 'good'
- 'warning'
- 'danger'
""" """
EXAMPLES = """ EXAMPLES = """
@ -111,14 +121,24 @@ EXAMPLES = """
link_names: 0 link_names: 0
parse: 'none' parse: 'none'
- name: insert a color bar in front of the message for visibility purposes and use the default webhook icon and name configured in Slack
slack:
domain: future500.slack.com
token: thetokengeneratedbyslack
msg: "{{ inventory_hostname }} is alive!"
color: good
username: ""
icon_url: ""
""" """
OLD_SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s' OLD_SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s'
SLACK_INCOMING_WEBHOOK = 'https://hooks.slack.com/services/%s' SLACK_INCOMING_WEBHOOK = 'https://hooks.slack.com/services/%s'
def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse): def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color):
payload = dict(text=text) if color == 'normal':
payload = dict(text=text)
else:
payload = dict(attachments=[dict(text=text, color=color)])
if channel is not None: if channel is not None:
payload['channel'] = channel if (channel[0] == '#') else '#'+channel payload['channel'] = channel if (channel[0] == '#') else '#'+channel
if username is not None: if username is not None:
@ -161,8 +181,8 @@ def main():
icon_emoji = dict(type='str', default=None), icon_emoji = dict(type='str', default=None),
link_names = dict(type='int', default=1, choices=[0,1]), link_names = dict(type='int', default=1, choices=[0,1]),
parse = dict(type='str', default=None, choices=['none', 'full']), parse = dict(type='str', default=None, choices=['none', 'full']),
validate_certs = dict(default='yes', type='bool'), validate_certs = dict(default='yes', type='bool'),
color = dict(type='str', default='normal', choices=['normal', 'good', 'warning', 'danger'])
) )
) )
@ -175,8 +195,9 @@ def main():
icon_emoji = module.params['icon_emoji'] icon_emoji = module.params['icon_emoji']
link_names = module.params['link_names'] link_names = module.params['link_names']
parse = module.params['parse'] parse = module.params['parse']
color = module.params['color']
payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse) payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color)
do_notify_slack(module, domain, token, payload) do_notify_slack(module, domain, token, payload)
module.exit_json(msg="OK") module.exit_json(msg="OK")

Loading…
Cancel
Save