From 66aa605367dd2ccca67f754f24b1af2e97a75c6d Mon Sep 17 00:00:00 2001 From: Phillip Holmes Date: Wed, 15 Oct 2014 21:14:10 -0500 Subject: [PATCH] added color bar option to Slack module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../modules/extras/notification/slack.py | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/extras/notification/slack.py b/lib/ansible/modules/extras/notification/slack.py index 1ae748247f..2b1459c3d9 100644 --- a/lib/ansible/modules/extras/notification/slack.py +++ b/lib/ansible/modules/extras/notification/slack.py @@ -89,6 +89,16 @@ options: choices: - 'yes' - '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 = """ @@ -111,14 +121,24 @@ EXAMPLES = """ link_names: 0 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' 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): - payload = dict(text=text) - +def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color): + if color == 'normal': + payload = dict(text=text) + else: + payload = dict(attachments=[dict(text=text, color=color)]) if channel is not None: payload['channel'] = channel if (channel[0] == '#') else '#'+channel if username is not None: @@ -161,8 +181,8 @@ def main(): icon_emoji = dict(type='str', default=None), link_names = dict(type='int', default=1, choices=[0,1]), parse = dict(type='str', default=None, choices=['none', 'full']), - 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'] link_names = module.params['link_names'] 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) module.exit_json(msg="OK")