Skip to content

Instantly share code, notes, and snippets.

@hrysd
Last active August 29, 2015 14:08
Show Gist options
  • Save hrysd/d10269472a2a62b6fb18 to your computer and use it in GitHub Desktop.
Save hrysd/d10269472a2a62b6fb18 to your computer and use it in GitHub Desktop.
Ansible module. Send notifcation to Idobata

Usage

$ wget https://gist.githubusercontent.com/hrysd/d10269472a2a62b6fb18/raw/c11247e6ea96305d68f3c1854912c216760c2d69/idobata
$ mv idobata /path/to/playbook/library

Refs

#!/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