2014-03-18 04:46:08 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-04-02 16:30:18 +02:00
|
|
|
# (c) 2015, Stefan Berggren <nsg@nsg.cc>
|
2014-03-18 04:46:08 +01:00
|
|
|
# (c) 2014, Ramon de la Fuente <ramon@delafuente.nl>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
module: slack
|
2014-03-18 04:59:15 +01:00
|
|
|
short_description: Send Slack notifications
|
2014-03-18 04:46:08 +01:00
|
|
|
description:
|
2014-03-18 04:59:15 +01:00
|
|
|
- The M(slack) module sends notifications to U(http://slack.com) via the Incoming WebHook integration
|
2015-10-08 10:18:23 +02:00
|
|
|
version_added: "1.6"
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Ramon de la Fuente (@ramondelafuente)"
|
2014-03-18 04:46:08 +01:00
|
|
|
options:
|
|
|
|
domain:
|
|
|
|
description:
|
2015-03-06 16:58:44 +01:00
|
|
|
- Slack (sub)domain for your environment without protocol. (i.e.
|
|
|
|
C(future500.slack.com)) In 1.8 and beyond, this is deprecated and may
|
|
|
|
be ignored. See token documentation for information.
|
|
|
|
required: false
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
token:
|
|
|
|
description:
|
2015-03-06 16:58:44 +01:00
|
|
|
- Slack integration token. This authenticates you to the slack service.
|
|
|
|
Prior to 1.8, a token looked like C(3Ffe373sfhRE6y42Fg3rvf4GlK). In
|
|
|
|
1.8 and above, ansible adapts to the new slack API where tokens look
|
|
|
|
like C(G922VJP24/D921DW937/3Ffe373sfhRE6y42Fg3rvf4GlK). If tokens
|
|
|
|
are in the new format then slack will ignore any value of domain. If
|
|
|
|
the token is in the old format the domain is required. Ansible has no
|
|
|
|
control of when slack will get rid of the old API. When slack does
|
|
|
|
that the old format will stop working.
|
2014-03-18 04:46:08 +01:00
|
|
|
required: true
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
- Message to send.
|
2015-04-02 16:30:18 +02:00
|
|
|
required: false
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
channel:
|
|
|
|
description:
|
|
|
|
- Channel to send the message to. If absent, the message goes to the channel selected for the I(token).
|
|
|
|
required: false
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
username:
|
|
|
|
description:
|
|
|
|
- This is the sender of the message.
|
|
|
|
required: false
|
2015-10-08 10:18:23 +02:00
|
|
|
default: "Ansible"
|
2014-03-18 04:46:08 +01:00
|
|
|
icon_url:
|
|
|
|
description:
|
2016-08-02 21:12:51 +02:00
|
|
|
- Url for the message sender's icon (default C(https://www.ansible.com/favicon.ico))
|
2014-03-18 04:46:08 +01:00
|
|
|
required: false
|
|
|
|
icon_emoji:
|
|
|
|
description:
|
|
|
|
- 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
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
link_names:
|
|
|
|
description:
|
|
|
|
- Automatically create links for channels and usernames in I(msg).
|
|
|
|
required: false
|
|
|
|
default: 1
|
|
|
|
choices:
|
|
|
|
- 1
|
|
|
|
- 0
|
|
|
|
parse:
|
|
|
|
description:
|
|
|
|
- Setting for the message parser at Slack
|
|
|
|
required: false
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
choices:
|
|
|
|
- 'full'
|
|
|
|
- 'none'
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices:
|
|
|
|
- 'yes'
|
|
|
|
- 'no'
|
2014-10-16 04:14:10 +02:00
|
|
|
color:
|
2015-10-08 10:18:23 +02:00
|
|
|
version_added: "2.0"
|
2014-10-16 04:14:10 +02:00
|
|
|
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'
|
2015-04-02 16:30:18 +02:00
|
|
|
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
|
2015-10-08 10:18:23 +02:00
|
|
|
default: None
|
2014-03-18 04:46:08 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Send notification message via Slack
|
|
|
|
local_action:
|
|
|
|
module: slack
|
2015-10-08 12:59:21 +02:00
|
|
|
token: thetoken/generatedby/slack
|
2014-03-18 04:46:08 +01:00
|
|
|
msg: "{{ inventory_hostname }} completed"
|
|
|
|
|
|
|
|
- name: Send notification message via Slack all options
|
|
|
|
local_action:
|
|
|
|
module: slack
|
2015-10-08 12:59:21 +02:00
|
|
|
token: thetoken/generatedby/slack
|
2014-03-18 04:46:08 +01:00
|
|
|
msg: "{{ inventory_hostname }} completed"
|
|
|
|
channel: "#ansible"
|
|
|
|
username: "Ansible on {{ inventory_hostname }}"
|
|
|
|
icon_url: "http://www.example.com/some-image-file.png"
|
|
|
|
link_names: 0
|
|
|
|
parse: 'none'
|
|
|
|
|
2014-10-16 04:14:10 +02:00
|
|
|
- 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:
|
2015-10-08 12:59:21 +02:00
|
|
|
token: thetoken/generatedby/slack
|
2014-10-16 04:14:10 +02:00
|
|
|
msg: "{{ inventory_hostname }} is alive!"
|
|
|
|
color: good
|
|
|
|
username: ""
|
|
|
|
icon_url: ""
|
2015-04-02 16:30:18 +02:00
|
|
|
|
|
|
|
- name: Use the attachments API
|
|
|
|
slack:
|
2015-10-08 12:59:21 +02:00
|
|
|
token: thetoken/generatedby/slack
|
2015-04-02 16:30:18 +02:00
|
|
|
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"
|
2015-10-08 12:59:21 +02:00
|
|
|
|
|
|
|
- name: Send notification message via Slack (deprecated API using domian)
|
|
|
|
local_action:
|
|
|
|
module: slack
|
|
|
|
domain: future500.slack.com
|
|
|
|
token: thetokengeneratedbyslack
|
|
|
|
msg: "{{ inventory_hostname }} completed"
|
|
|
|
|
2014-03-18 04:46:08 +01:00
|
|
|
"""
|
|
|
|
|
2015-03-06 16:58:44 +01:00
|
|
|
OLD_SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s'
|
2014-11-06 22:09:26 +01:00
|
|
|
SLACK_INCOMING_WEBHOOK = 'https://hooks.slack.com/services/%s'
|
2014-03-18 04:46:08 +01:00
|
|
|
|
2015-04-02 16:30:18 +02:00
|
|
|
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:
|
2014-10-16 04:14:10 +02:00
|
|
|
payload = dict(text=text)
|
2015-04-02 16:30:18 +02:00
|
|
|
elif text is not None:
|
2014-10-16 04:14:10 +02:00
|
|
|
payload = dict(attachments=[dict(text=text, color=color)])
|
2014-03-18 04:46:08 +01:00
|
|
|
if channel is not None:
|
2015-05-08 23:36:15 +02:00
|
|
|
if (channel[0] == '#') or (channel[0] == '@'):
|
2015-01-22 15:05:32 +01:00
|
|
|
payload['channel'] = channel
|
2015-05-08 23:36:15 +02:00
|
|
|
else:
|
2015-01-22 15:05:32 +01:00
|
|
|
payload['channel'] = '#'+channel
|
2014-03-18 04:46:08 +01:00
|
|
|
if username is not None:
|
|
|
|
payload['username'] = username
|
|
|
|
if icon_emoji is not None:
|
|
|
|
payload['icon_emoji'] = icon_emoji
|
|
|
|
else:
|
|
|
|
payload['icon_url'] = icon_url
|
|
|
|
if link_names is not None:
|
|
|
|
payload['link_names'] = link_names
|
|
|
|
if parse is not None:
|
|
|
|
payload['parse'] = parse
|
|
|
|
|
2015-04-02 16:30:18 +02:00
|
|
|
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)
|
|
|
|
|
2014-03-18 04:46:08 +01:00
|
|
|
payload="payload=" + module.jsonify(payload)
|
|
|
|
return payload
|
|
|
|
|
|
|
|
def do_notify_slack(module, domain, token, payload):
|
2015-03-06 16:58:44 +01:00
|
|
|
if token.count('/') >= 2:
|
|
|
|
# New style token
|
|
|
|
slack_incoming_webhook = SLACK_INCOMING_WEBHOOK % (token)
|
|
|
|
else:
|
|
|
|
if not domain:
|
|
|
|
module.fail_json(msg="Slack has updated its webhook API. You need to specify a token of the form XXXX/YYYY/ZZZZ in your playbook")
|
|
|
|
slack_incoming_webhook = OLD_SLACK_INCOMING_WEBHOOK % (domain, token)
|
2014-03-18 04:46:08 +01:00
|
|
|
|
|
|
|
response, info = fetch_url(module, slack_incoming_webhook, data=payload)
|
|
|
|
if info['status'] != 200:
|
2014-11-06 22:09:26 +01:00
|
|
|
obscured_incoming_webhook = SLACK_INCOMING_WEBHOOK % ('[obscured]')
|
2014-03-18 04:46:08 +01:00
|
|
|
module.fail_json(msg=" failed to send %s to %s: %s" % (payload, obscured_incoming_webhook, info['msg']))
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2015-03-06 16:58:44 +01:00
|
|
|
domain = dict(type='str', required=False, default=None),
|
2015-07-08 19:13:12 +02:00
|
|
|
token = dict(type='str', required=True, no_log=True),
|
2015-04-02 16:30:18 +02:00
|
|
|
msg = dict(type='str', required=False, default=None),
|
2014-03-18 04:46:08 +01:00
|
|
|
channel = dict(type='str', default=None),
|
|
|
|
username = dict(type='str', default='Ansible'),
|
2016-08-02 21:12:51 +02:00
|
|
|
icon_url = dict(type='str', default='https://www.ansible.com/favicon.ico'),
|
2014-03-18 04:46:08 +01:00
|
|
|
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'),
|
2015-04-02 16:30:18 +02:00
|
|
|
color = dict(type='str', default='normal', choices=['normal', 'good', 'warning', 'danger']),
|
|
|
|
attachments = dict(type='list', required=False, default=None)
|
2014-03-18 04:46:08 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
domain = module.params['domain']
|
|
|
|
token = module.params['token']
|
|
|
|
text = module.params['msg']
|
|
|
|
channel = module.params['channel']
|
|
|
|
username = module.params['username']
|
|
|
|
icon_url = module.params['icon_url']
|
|
|
|
icon_emoji = module.params['icon_emoji']
|
|
|
|
link_names = module.params['link_names']
|
|
|
|
parse = module.params['parse']
|
2014-10-16 04:14:10 +02:00
|
|
|
color = module.params['color']
|
2015-04-02 16:30:18 +02:00
|
|
|
attachments = module.params['attachments']
|
2014-03-18 04:46:08 +01:00
|
|
|
|
2015-04-02 16:30:18 +02:00
|
|
|
payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse, color, attachments)
|
2014-03-18 04:46:08 +01:00
|
|
|
do_notify_slack(module, domain, token, payload)
|
|
|
|
|
|
|
|
module.exit_json(msg="OK")
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.urls import *
|
2015-10-08 10:18:23 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|