1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add attachments support to slack module.

This commit is contained in:
Stefan Berggren 2015-04-02 16:30:18 +02:00 committed by Matt Clay
parent b43329b6a0
commit aeb1090542

View file

@ -1,6 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2015, Stefan Berggren <nsg@nsg.cc>
# (c) 2014, Ramon de la Fuente <ramon@delafuente.nl> # (c) 2014, Ramon de la Fuente <ramon@delafuente.nl>
# #
# This file is part of Ansible # This file is part of Ansible
@ -46,7 +47,7 @@ options:
msg: msg:
description: description:
- Message to send. - Message to send.
required: true required: false
channel: channel:
description: description:
- Channel to send the message to. If absent, the message goes to the channel selected for the I(token). - 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' - 'good'
- 'warning' - 'warning'
- 'danger' - '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 = """ EXAMPLES = """
@ -130,15 +135,32 @@ EXAMPLES = """
color: good color: good
username: "" username: ""
icon_url: "" 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' 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, color): def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color, attachments):
if color == 'normal': payload = {}
if color == "normal" and text is not None:
payload = dict(text=text) payload = dict(text=text)
else: elif text is not None:
payload = dict(attachments=[dict(text=text, color=color)]) payload = dict(attachments=[dict(text=text, color=color)])
if channel is not None: if channel is not None:
if (channel[0] == '#') or (channel[0] == '@'): 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: if parse is not None:
payload['parse'] = parse 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) payload="payload=" + module.jsonify(payload)
return payload return payload
@ -178,7 +210,7 @@ def main():
argument_spec = dict( argument_spec = dict(
domain = dict(type='str', required=False, default=None), domain = dict(type='str', required=False, default=None),
token = dict(type='str', required=True, no_log=True), 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), channel = dict(type='str', default=None),
username = dict(type='str', default='Ansible'), username = dict(type='str', default='Ansible'),
icon_url = dict(type='str', default='http://www.ansible.com/favicon.ico'), 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]), 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']) 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'] link_names = module.params['link_names']
parse = module.params['parse'] parse = module.params['parse']
color = module.params['color'] 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) do_notify_slack(module, domain, token, payload)
module.exit_json(msg="OK") module.exit_json(msg="OK")