From fbac5a140ef1ec23d2eb2bba40f2bfc7fbd63c56 Mon Sep 17 00:00:00 2001 From: Stefan Berggren Date: Thu, 2 Apr 2015 16:30:18 +0200 Subject: [PATCH 1/3] Add attachments support to slack module. --- notification/slack.py | 48 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/notification/slack.py b/notification/slack.py index ba4ed2e4c2d..96102b3d5fe 100644 --- a/notification/slack.py +++ b/notification/slack.py @@ -1,6 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +# (c) 2015, Stefan Berggren # (c) 2014, Ramon de la Fuente # # This file is part of Ansible @@ -46,7 +47,7 @@ options: msg: description: - Message to send. - required: true + required: false channel: description: - Channel to send the message to. If absent, the message goes to the channel selected for the I(token). @@ -100,6 +101,10 @@ options: - 'good' - 'warning' - 'danger' + attachments: + description: + - Define a list of attachments. This list mirrors the Slack JSON API. For more information, see https://api.slack.com/docs/attachments + required: false """ EXAMPLES = """ @@ -130,15 +135,32 @@ EXAMPLES = """ color: good username: "" icon_url: "" + +- name: Use the attachments API + slack: + domain: future500.slack.com + token: thetokengeneratedbyslack + attachments: + - text: "Display my system load on host A and B" + color: "#ff00dd" + title: "System load" + fields: + - title: "System A" + value: "load average: 0,74, 0,66, 0,63" + short: "true" + - title: "System B" + value: "load average: 5,16, 4,64, 2,43" + short: "true" """ 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, color): - if color == 'normal': +def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color, attachments): + payload = {} + if color == "normal" and text is not None: payload = dict(text=text) - else: + elif text is not None: payload = dict(attachments=[dict(text=text, color=color)]) if channel is not None: if (channel[0] == '#') or (channel[0] == '@'): @@ -156,6 +178,16 @@ def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoj if parse is not None: payload['parse'] = parse + if attachments is not None: + if 'attachments' not in payload: + payload['attachments'] = [] + + if attachments is not None: + for attachment in attachments: + if 'fallback' not in attachment: + attachment['fallback'] = attachment['text'] + payload['attachments'].append(attachment) + payload="payload=" + module.jsonify(payload) return payload @@ -178,7 +210,7 @@ def main(): argument_spec = dict( domain = dict(type='str', required=False, default=None), token = dict(type='str', required=True, no_log=True), - msg = dict(type='str', required=True), + msg = dict(type='str', required=False, default=None), channel = dict(type='str', default=None), username = dict(type='str', default='Ansible'), icon_url = dict(type='str', default='http://www.ansible.com/favicon.ico'), @@ -186,7 +218,8 @@ def main(): 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']) + color = dict(type='str', default='normal', choices=['normal', 'good', 'warning', 'danger']), + attachments = dict(type='list', required=False, default=None) ) ) @@ -200,8 +233,9 @@ def main(): link_names = module.params['link_names'] parse = module.params['parse'] color = module.params['color'] + attachments = module.params['attachments'] - payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color) + payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color, attachments) do_notify_slack(module, domain, token, payload) module.exit_json(msg="OK") From 8ffc11713f362b4905ae91dc218723183ad21ae0 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Thu, 8 Oct 2015 10:18:23 +0200 Subject: [PATCH 2/3] module guidelines compliency --- notification/slack.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/notification/slack.py b/notification/slack.py index 96102b3d5fe..de75584fc48 100644 --- a/notification/slack.py +++ b/notification/slack.py @@ -24,7 +24,7 @@ module: slack short_description: Send Slack notifications description: - The M(slack) module sends notifications to U(http://slack.com) via the Incoming WebHook integration -version_added: 1.6 +version_added: "1.6" author: "Ramon de la Fuente (@ramondelafuente)" options: domain: @@ -33,6 +33,7 @@ options: C(future500.slack.com)) In 1.8 and beyond, this is deprecated and may be ignored. See token documentation for information. required: false + default: None token: description: - Slack integration token. This authenticates you to the slack service. @@ -48,15 +49,17 @@ options: description: - Message to send. required: false + default: None channel: description: - Channel to send the message to. If absent, the message goes to the channel selected for the I(token). required: false + default: None username: description: - This is the sender of the message. required: false - default: ansible + default: "Ansible" icon_url: description: - Url for the message sender's icon (default C(http://www.ansible.com/favicon.ico)) @@ -66,6 +69,7 @@ options: - Emoji for the message sender. See Slack documentation for options. (if I(icon_emoji) is set, I(icon_url) will not be used) required: false + default: None link_names: description: - Automatically create links for channels and usernames in I(msg). @@ -78,6 +82,7 @@ options: description: - Setting for the message parser at Slack required: false + default: None choices: - 'full' - 'none' @@ -91,7 +96,7 @@ options: - 'yes' - 'no' color: - version_added: 2.0 + version_added: "2.0" 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 @@ -105,6 +110,7 @@ options: description: - Define a list of attachments. This list mirrors the Slack JSON API. For more information, see https://api.slack.com/docs/attachments required: false + default: None """ EXAMPLES = """ @@ -243,4 +249,6 @@ def main(): # import module snippets from ansible.module_utils.basic import * from ansible.module_utils.urls import * -main() + +if __name__ == '__main__': + main() From d14bb33a03b49e6f51205b88ae01d70875869b51 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Thu, 8 Oct 2015 12:59:21 +0200 Subject: [PATCH 3/3] updated the examples to the new Slack API --- notification/slack.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/notification/slack.py b/notification/slack.py index de75584fc48..e01272c0703 100644 --- a/notification/slack.py +++ b/notification/slack.py @@ -117,15 +117,13 @@ EXAMPLES = """ - name: Send notification message via Slack local_action: module: slack - domain: future500.slack.com - token: thetokengeneratedbyslack + token: thetoken/generatedby/slack msg: "{{ inventory_hostname }} completed" - name: Send notification message via Slack all options local_action: module: slack - domain: future500.slack.com - token: thetokengeneratedbyslack + token: thetoken/generatedby/slack msg: "{{ inventory_hostname }} completed" channel: "#ansible" username: "Ansible on {{ inventory_hostname }}" @@ -135,8 +133,7 @@ EXAMPLES = """ - 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 + token: thetoken/generatedby/slack msg: "{{ inventory_hostname }} is alive!" color: good username: "" @@ -144,8 +141,7 @@ EXAMPLES = """ - name: Use the attachments API slack: - domain: future500.slack.com - token: thetokengeneratedbyslack + token: thetoken/generatedby/slack attachments: - text: "Display my system load on host A and B" color: "#ff00dd" @@ -157,6 +153,14 @@ EXAMPLES = """ - title: "System B" value: "load average: 5,16, 4,64, 2,43" short: "true" + +- name: Send notification message via Slack (deprecated API using domian) + local_action: + module: slack + domain: future500.slack.com + token: thetokengeneratedbyslack + msg: "{{ inventory_hostname }} completed" + """ OLD_SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s'