ansible/notification/campfire.py

126 lines
4.4 KiB
Python
Raw Normal View History

#!/usr/bin/python
2013-05-10 21:17:41 +02:00
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: campfire
version_added: "1.2"
2013-05-10 21:17:41 +02:00
short_description: Send a message to Campfire
description:
- Send a message to Campfire.
2013-05-11 22:33:49 +02:00
- Messages with newlines will result in a "Paste" message being sent.
2013-05-10 21:17:41 +02:00
version_added: "1.2"
options:
subscription:
description:
- The subscription name to use.
required: true
token:
description:
- API token.
required: true
room:
description:
- Room number to which the message should be sent.
required: true
msg:
description:
- The message body.
required: true
notify:
description:
- Send a notification sound before the message.
required: false
choices: ["56k", "bell", "bezos", "bueller", "clowntown",
"cottoneyejoe", "crickets", "dadgummit", "dangerzone",
"danielsan", "deeper", "drama", "greatjob", "greyjoy",
"guarantee", "heygirl", "horn", "horror",
"inconceivable", "live", "loggins", "makeitso", "noooo",
"nyan", "ohmy", "ohyeah", "pushit", "rimshot",
"rollout", "rumble", "sax", "secret", "sexyback",
"story", "tada", "tmyk", "trololo", "trombone", "unix",
"vuvuzela", "what", "whoomp", "yeah", "yodel"]
2013-05-10 21:17:41 +02:00
# informational: requirements for nodes
2015-07-21 08:28:29 +02:00
requirements: [ ]
author: "Adam Garside (@fabulops)"
2013-05-10 21:17:41 +02:00
'''
EXAMPLES = '''
- campfire: subscription=foo token=12345 room=123 msg="Task completed."
2013-05-10 21:17:41 +02:00
- campfire: subscription=foo token=12345 room=123 notify=loggins
2013-05-10 21:17:41 +02:00
msg="Task completed ... with feeling."
'''
2015-07-21 08:28:29 +02:00
import cgi
2013-05-10 21:17:41 +02:00
def main():
module = AnsibleModule(
argument_spec=dict(
subscription=dict(required=True),
token=dict(required=True),
room=dict(required=True),
msg=dict(required=True),
notify=dict(required=False,
choices=["56k", "bell", "bezos", "bueller",
"clowntown", "cottoneyejoe",
"crickets", "dadgummit", "dangerzone",
"danielsan", "deeper", "drama",
"greatjob", "greyjoy", "guarantee",
"heygirl", "horn", "horror",
2013-05-10 21:17:41 +02:00
"inconceivable", "live", "loggins",
"makeitso", "noooo", "nyan", "ohmy",
"ohyeah", "pushit", "rimshot",
"rollout", "rumble", "sax", "secret",
"sexyback", "story", "tada", "tmyk",
"trololo", "trombone", "unix",
"vuvuzela", "what", "whoomp", "yeah",
"yodel"]),
2013-05-10 21:17:41 +02:00
),
supports_check_mode=False
2013-05-10 21:17:41 +02:00
)
subscription = module.params["subscription"]
token = module.params["token"]
room = module.params["room"]
msg = module.params["msg"]
notify = module.params["notify"]
URI = "https://%s.campfirenow.com" % subscription
NSTR = "<message><type>SoundMessage</type><body>%s</body></message>"
MSTR = "<message><body>%s</body></message>"
AGENT = "Ansible/1.2"
2015-07-21 08:28:29 +02:00
# Hack to add basic auth username and password the way fetch_url expects
module.params['url_username'] = token
module.params['url_password'] = 'X'
2015-07-21 08:28:29 +02:00
target_url = '%s/room/%s/speak.xml' % (URI, room)
headers = {'Content-Type': 'application/xml',
'User-agent': AGENT}
# Send some audible notification if requested
if notify:
response, info = fetch_url(module, target_url, data=NSTR % cgi.escape(notify), headers=headers)
if info['status'] != 200:
module.fail_json(msg="unable to send msg: '%s', campfire api"
" returned error code: '%s'" %
(notify, info['status']))
# Send the message
response, info = fetch_url(module, target_url, data=MSTR %cgi.escape(msg), headers=headers)
if info['status'] != 200:
module.fail_json(msg="unable to send msg: '%s', campfire api"
" returned error code: '%s'" %
(msg, info['status']))
2013-05-10 21:17:41 +02:00
module.exit_json(changed=True, room=room, msg=msg, notify=notify)
2013-12-02 21:13:49 +01:00
# import module snippets
from ansible.module_utils.basic import *
2015-07-21 08:28:29 +02:00
from ansible.module_utils.urls import *
if __name__ == '__main__':
main()