mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.0 KiB
Python
150 lines
4.0 KiB
Python
12 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: hipchat
|
||
12 years ago
|
version_added: "1.2"
|
||
12 years ago
|
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
|
||
12 years ago
|
default: 'yes'
|
||
|
choices: [ "yes", "no" ]
|
||
11 years ago
|
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
|
||
11 years ago
|
api:
|
||
|
description:
|
||
|
- API url if using a self-hosted hipchat server
|
||
|
required: false
|
||
|
default: 'https://api.hipchat.com/v1/rooms/message'
|
||
|
version_added: 1.6.0
|
||
|
|
||
12 years ago
|
|
||
|
# informational: requirements for nodes
|
||
|
requirements: [ urllib, urllib2 ]
|
||
|
author: WAKAYAMA Shirou
|
||
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
12 years ago
|
- hipchat: token=AAAAAA room=notify msg="Ansible task finished"
|
||
12 years ago
|
'''
|
||
|
|
||
|
# ===========================================
|
||
|
# HipChat module specific support methods.
|
||
|
#
|
||
|
|
||
11 years ago
|
MSG_URI = "https://api.hipchat.com/v1/rooms/message"
|
||
|
|
||
11 years ago
|
def send_msg(module, token, room, msg_from, msg, msg_format='text',
|
||
11 years ago
|
color='yellow', notify=False, api=MSG_URI):
|
||
12 years ago
|
'''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
|
||
11 years ago
|
params['api'] = api
|
||
12 years ago
|
|
||
|
if notify:
|
||
|
params['notify'] = 1
|
||
|
else:
|
||
|
params['notify'] = 0
|
||
|
|
||
11 years ago
|
url = api + "?auth_token=%s" % (token)
|
||
11 years ago
|
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']))
|
||
12 years ago
|
|
||
|
|
||
|
# ===========================================
|
||
|
# Module execution.
|
||
|
#
|
||
|
|
||
|
def main():
|
||
|
|
||
|
module = AnsibleModule(
|
||
|
argument_spec=dict(
|
||
|
token=dict(required=True),
|
||
|
room=dict(required=True),
|
||
|
msg=dict(required=True),
|
||
11 years ago
|
msg_from=dict(default="Ansible", aliases=['from']),
|
||
12 years ago
|
color=dict(default="yellow", choices=["yellow", "red", "green",
|
||
|
"purple", "gray", "random"]),
|
||
|
msg_format=dict(default="text", choices=["text", "html"]),
|
||
12 years ago
|
notify=dict(default=True, type='bool'),
|
||
11 years ago
|
validate_certs = dict(default='yes', type='bool'),
|
||
11 years ago
|
api = dict(default=MSG_URI),
|
||
12 years ago
|
),
|
||
|
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"]
|
||
11 years ago
|
api = module.params["api"]
|
||
12 years ago
|
|
||
|
try:
|
||
11 years ago
|
send_msg(module, token, room, msg_from, msg, msg_format, color, notify, api)
|
||
12 years ago
|
except Exception, e:
|
||
|
module.fail_json(msg="unable to sent msg: %s" % e)
|
||
|
|
||
|
changed = True
|
||
11 years ago
|
module.exit_json(changed=changed, room=room, msg_from=msg_from, msg=msg)
|
||
12 years ago
|
|
||
11 years ago
|
# import module snippets
|
||
11 years ago
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
from ansible.module_utils.urls import *
|
||
|
|
||
12 years ago
|
main()
|