mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
5bc6eafba5
* Adds another module utility file which generalizes the access of urls via the urllib* libraries. * Adds a new spec generator for common arguments. * Makes the user-agent string configurable. Fixes #6211
90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: grove
|
|
version_added: 1.4
|
|
short_description: Sends a notification to a grove.io channel
|
|
description:
|
|
- The M(grove) module sends a message for a service to a Grove.io
|
|
channel.
|
|
options:
|
|
channel_token:
|
|
description:
|
|
- Token of the channel to post to.
|
|
required: true
|
|
service:
|
|
description:
|
|
- Name of the service (displayed as the "user" in the message)
|
|
required: false
|
|
default: ansible
|
|
message:
|
|
description:
|
|
- Message content
|
|
required: true
|
|
url:
|
|
description:
|
|
- Service URL for the web client
|
|
required: false
|
|
icon_url:
|
|
description:
|
|
- Icon for the service
|
|
required: false
|
|
author: Jonas Pfenniger <zimbatm@zimbatm.com>
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- grove: >
|
|
channel_token=6Ph62VBBJOccmtTPZbubiPzdrhipZXtg
|
|
service=my-app
|
|
message=deployed {{ target }}
|
|
'''
|
|
|
|
BASE_URL = 'https://grove.io/api/notice/%s/'
|
|
|
|
# ==============================================================
|
|
# do_notify_grove
|
|
|
|
def do_notify_grove(module, channel_token, service, message, url=None, icon_url=None):
|
|
my_url = BASE_URL % (channel_token,)
|
|
|
|
my_data = dict(service=service, message=message)
|
|
if url is not None:
|
|
my_data['url'] = url
|
|
if icon_url is not None:
|
|
my_data['icon_url'] = icon_url
|
|
|
|
data = urllib.urlencode(my_data)
|
|
response, info = fetch_url(module, my_url, data=data)
|
|
if info['status'] != 200:
|
|
module.fail_json(msg="failed to send notification: %s" % info['msg'])
|
|
|
|
# ==============================================================
|
|
# main
|
|
|
|
def main():
|
|
module = AnsibleModule(
|
|
argument_spec = dict(
|
|
channel_token = dict(type='str', required=True),
|
|
message = dict(type='str', required=True),
|
|
service = dict(type='str', default='ansible'),
|
|
url = dict(type='str', default=None),
|
|
icon_url = dict(type='str', default=None),
|
|
)
|
|
)
|
|
|
|
channel_token = module.params['channel_token']
|
|
service = module.params['service']
|
|
message = module.params['message']
|
|
url = module.params['url']
|
|
icon_url = module.params['icon_url']
|
|
|
|
do_notify_grove(module, channel_token, service, message, url, icon_url)
|
|
|
|
# Mission complete
|
|
module.exit_json(msg="OK")
|
|
|
|
# import module snippets
|
|
from ansible.module_utils.basic import *
|
|
main()
|