summaryrefslogtreecommitdiff
path: root/notification
diff options
context:
space:
mode:
authorStefan Berggren <nsg@nsg.cc>2015-04-02 16:30:18 +0200
committerStefan Berggren <nsg@nsg.cc>2015-07-11 10:58:30 +0200
commitfbac5a140ef1ec23d2eb2bba40f2bfc7fbd63c56 (patch)
treeda9e4ec4362fb1b14d1ab0b9aab3b05e7d396343 /notification
parent5713cc5ee9f9913c5a455a10a612502c5b1ecc72 (diff)
downloadansible-modules-extras-fbac5a140ef1ec23d2eb2bba40f2bfc7fbd63c56.tar.gz
Add attachments support to slack module.
Diffstat (limited to 'notification')
-rw-r--r--notification/slack.py48
1 files changed, 41 insertions, 7 deletions
diff --git a/notification/slack.py b/notification/slack.py
index ba4ed2e4..96102b3d 100644
--- a/notification/slack.py
+++ b/notification/slack.py
@@ -1,6 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
+# (c) 2015, Stefan Berggren <nsg@nsg.cc>
# (c) 2014, Ramon de la Fuente <ramon@delafuente.nl>
#
# 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")