2013-07-17 22:37:04 +02:00
|
|
|
#!/usr/bin/python
|
2013-05-02 16:23:40 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: hipchat
|
2013-05-11 22:36:37 +02:00
|
|
|
version_added: "1.2"
|
2013-05-02 16:23:40 +02:00
|
|
|
short_description: Send a message to hipchat
|
|
|
|
description:
|
|
|
|
- Send a message to hipchat
|
|
|
|
options:
|
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- API token.
|
|
|
|
required: true
|
|
|
|
room:
|
|
|
|
description:
|
|
|
|
- ID or name of the room.
|
|
|
|
required: true
|
|
|
|
from:
|
|
|
|
description:
|
|
|
|
- Name the message will appear be sent from. max 15 characters.
|
|
|
|
Over 15, will be shorten.
|
|
|
|
required: false
|
|
|
|
default: Ansible
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
- The message body.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
color:
|
|
|
|
description:
|
|
|
|
- Background color for the message. Default is yellow.
|
|
|
|
required: false
|
|
|
|
default: yellow
|
|
|
|
choices: [ "yellow", "red", "green", "purple", "gray", "random" ]
|
|
|
|
msg_format:
|
|
|
|
description:
|
|
|
|
- message format. html or text. Default is text.
|
|
|
|
required: false
|
|
|
|
default: text
|
|
|
|
choices: [ "text", "html" ]
|
|
|
|
notify:
|
|
|
|
description:
|
|
|
|
- notify or not (change the tab color, play a sound, etc)
|
|
|
|
required: false
|
2013-06-01 18:14:35 +02:00
|
|
|
default: 'yes'
|
|
|
|
choices: [ "yes", "no" ]
|
2014-03-12 16:19:22 +01:00
|
|
|
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']
|
|
|
|
version_added: 1.5.1
|
2013-05-02 16:23:40 +02:00
|
|
|
|
|
|
|
# informational: requirements for nodes
|
|
|
|
requirements: [ urllib, urllib2 ]
|
|
|
|
author: WAKAYAMA Shirou
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-06-17 08:53:46 +02:00
|
|
|
- hipchat: token=AAAAAA room=notify msg="Ansible task finished"
|
2013-05-02 16:23:40 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# HipChat module specific support methods.
|
|
|
|
#
|
|
|
|
|
|
|
|
MSG_URI = "https://api.hipchat.com/v1/rooms/message?"
|
|
|
|
|
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
def send_msg(module, token, room, msg_from, msg, msg_format='text',
|
2013-05-02 16:23:40 +02:00
|
|
|
color='yellow', notify=False):
|
|
|
|
'''sending message to hipchat'''
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
params['room_id'] = room
|
|
|
|
params['from'] = msg_from[:15] # max length is 15
|
|
|
|
params['message'] = msg
|
|
|
|
params['message_format'] = msg_format
|
|
|
|
params['color'] = color
|
|
|
|
|
|
|
|
if notify:
|
|
|
|
params['notify'] = 1
|
|
|
|
else:
|
|
|
|
params['notify'] = 0
|
|
|
|
|
|
|
|
url = MSG_URI + "auth_token=%s" % (token)
|
2014-03-10 22:06:52 +01:00
|
|
|
data = urllib.urlencode(params)
|
|
|
|
response, info = fetch_url(module, url, data=data)
|
|
|
|
if info['status'] == 200:
|
|
|
|
return response.read()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="failed to send message, return status=%s" % str(info['status']))
|
2013-05-02 16:23:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
token=dict(required=True),
|
|
|
|
room=dict(required=True),
|
|
|
|
msg=dict(required=True),
|
2013-10-07 14:14:54 +02:00
|
|
|
msg_from=dict(default="Ansible", aliases=['from']),
|
2013-05-02 16:23:40 +02:00
|
|
|
color=dict(default="yellow", choices=["yellow", "red", "green",
|
|
|
|
"purple", "gray", "random"]),
|
|
|
|
msg_format=dict(default="text", choices=["text", "html"]),
|
2013-06-01 18:14:35 +02:00
|
|
|
notify=dict(default=True, type='bool'),
|
2014-03-12 16:19:22 +01:00
|
|
|
validate_certs = dict(default='yes', type='bool'),
|
2013-05-02 16:23:40 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
token = module.params["token"]
|
|
|
|
room = module.params["room"]
|
|
|
|
msg = module.params["msg"]
|
|
|
|
msg_from = module.params["msg_from"]
|
|
|
|
color = module.params["color"]
|
|
|
|
msg_format = module.params["msg_format"]
|
|
|
|
notify = module.params["notify"]
|
|
|
|
|
|
|
|
try:
|
2014-03-10 22:06:52 +01:00
|
|
|
send_msg(module, token, room, msg_from, msg, msg_format, color, notify)
|
2013-05-02 16:23:40 +02:00
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="unable to sent msg: %s" % e)
|
|
|
|
|
|
|
|
changed = True
|
2014-03-10 22:06:52 +01:00
|
|
|
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
|
2013-05-02 16:23:40 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2014-03-10 22:06:52 +01:00
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
|
2013-05-02 16:23:40 +02:00
|
|
|
main()
|