2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: grove
|
|
|
|
short_description: Sends a notification to a grove.io channel
|
|
|
|
description:
|
|
|
|
- The C(grove) module sends a message for a service to a Grove.io
|
|
|
|
channel.
|
|
|
|
options:
|
|
|
|
channel_token:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Token of the channel to post to.
|
|
|
|
required: true
|
|
|
|
service:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Name of the service (displayed as the "user" in the message)
|
|
|
|
required: false
|
|
|
|
default: ansible
|
2021-02-27 21:23:14 +01:00
|
|
|
message_content:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-02-27 21:23:14 +01:00
|
|
|
- Message content.
|
|
|
|
- The alias I(message) is deprecated and will be removed in community.general 4.0.0.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
url:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Service URL for the web client
|
|
|
|
required: false
|
|
|
|
icon_url:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Icon for the service
|
|
|
|
required: false
|
|
|
|
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.
|
|
|
|
default: 'yes'
|
|
|
|
type: bool
|
|
|
|
author: "Jonas Pfenniger (@zimbatm)"
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Sends a notification to a grove.io channel
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.grove:
|
2020-05-15 12:12:41 +02:00
|
|
|
channel_token: 6Ph62VBBJOccmtTPZbubiPzdrhipZXtg
|
|
|
|
service: my-app
|
|
|
|
message: 'deployed {{ target }}'
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
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 = 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, no_log=True),
|
2021-10-12 13:56:15 +02:00
|
|
|
message_content=dict(type='str', required=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
service=dict(type='str', default='ansible'),
|
|
|
|
url=dict(type='str', default=None),
|
|
|
|
icon_url=dict(type='str', default=None),
|
2020-06-22 15:56:35 +02:00
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
channel_token = module.params['channel_token']
|
|
|
|
service = module.params['service']
|
2021-02-27 21:23:14 +01:00
|
|
|
message = module.params['message_content']
|
2020-03-09 10:11:07 +01:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|