$ wget https://gist.githubusercontent.com/hrysd/d10269472a2a62b6fb18/raw/c11247e6ea96305d68f3c1854912c216760c2d69/idobata
$ mv idobata /path/to/playbook/library
Last active
August 29, 2015 14:08
-
-
Save hrysd/d10269472a2a62b6fb18 to your computer and use it in GitHub Desktop.
Ansible module. Send notifcation to Idobata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
DOCUMENTATION = """ | |
module: idobata | |
short_description: Send notification to Idobata | |
description: | |
- Idobata Integration | |
author: hrysd <[email protected]> | |
options: | |
token: | |
description: | |
- token | |
required: true | |
message_format: | |
description: | |
- plain text or HTML (default: plain) | |
message: | |
description: | |
- Message to send. | |
required: true | |
""" | |
EXAMPLES = """ | |
- name: Send notification to Idobata | |
local_action: | |
module: idobata | |
token: hoge_huga_piyo | |
message: "{{ inventory_hostname }} completed :rocket:" | |
""" | |
HOOK_ENDPOINT = 'https://idobata.io/hook/generic/%s' | |
def build_payload(message, message_format): | |
payload = dict(source=message) | |
if message_format is 'html': | |
payload['format'] = message_format | |
else: | |
payload['format'] = 'plain' | |
return payload | |
def notify(module, token, payload): | |
try: | |
import urllib | |
import urllib2 | |
except ImportError: | |
module.fail_json(msg='`urllib2` is required') | |
endpoint = HOOK_ENDPOINT % token | |
params = urllib.urlencode(payload) | |
try: | |
urllib2.urlopen(endpoint, params).read() | |
except urllib2.HTTPError, e: | |
module.fail_json(msg = 'Failed to send webhook to Idobata, Error code is %s' % e.code) | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
token = dict(type='str', required=True), | |
message = dict(type='str', required=True), | |
message_format = dict(type='str', default='plain') | |
) | |
) | |
token = module.params['token'] | |
message = module.params['message'] | |
message_format = module.params['message_format'] | |
payload = build_payload(message, message_format) | |
notify(module, token, payload) | |
module.exit_json(msg = 'Success', changed = False) | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment