2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2012, Jim Richardson <weaselkeeper@gmail.com>
|
2020-09-03 08:01:52 +02:00
|
|
|
# Copyright (c) 2019, Bernd Arnold <wopfel@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# 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: pushover
|
|
|
|
short_description: Send notifications via U(https://pushover.net)
|
|
|
|
description:
|
2023-02-20 17:29:14 +01:00
|
|
|
- Send notifications via pushover, to subscriber list of devices, and email
|
|
|
|
addresses. Requires pushover app on devices.
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
2023-02-20 17:29:14 +01:00
|
|
|
- You will require a pushover.net account to use this module. But no account
|
|
|
|
is required to receive messages.
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
msg:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- What message you wish to send.
|
|
|
|
required: true
|
|
|
|
app_token:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Pushover issued token identifying your pushover app.
|
|
|
|
required: true
|
|
|
|
user_key:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Pushover issued authentication key for your user.
|
|
|
|
required: true
|
|
|
|
title:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Message title.
|
|
|
|
required: false
|
|
|
|
pri:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Message priority (see U(https://pushover.net) for details).
|
|
|
|
required: false
|
2020-11-05 06:50:47 +01:00
|
|
|
default: '0'
|
|
|
|
choices: [ '-2', '-1', '0', '1', '2' ]
|
2020-09-03 08:01:52 +02:00
|
|
|
device:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-09-03 08:01:52 +02:00
|
|
|
description:
|
|
|
|
- A device the message should be sent to. Multiple devices can be specified, separated by a comma.
|
|
|
|
required: false
|
|
|
|
version_added: 1.2.0
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-09-03 08:01:52 +02:00
|
|
|
author:
|
|
|
|
- "Jim Richardson (@weaselkeeper)"
|
|
|
|
- "Bernd Arnold (@wopfel)"
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Send notifications via pushover.net
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pushover:
|
2020-03-09 10:11:07 +01:00
|
|
|
msg: '{{ inventory_hostname }} is acting strange ...'
|
|
|
|
app_token: wxfdksl
|
|
|
|
user_key: baa5fe97f2c5ab3ca8f0bb59
|
|
|
|
delegate_to: localhost
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Send notifications via pushover.net
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pushover:
|
2020-03-09 10:11:07 +01:00
|
|
|
title: 'Alert!'
|
|
|
|
msg: '{{ inventory_hostname }} has exploded in flames, It is now time to panic'
|
|
|
|
pri: 1
|
|
|
|
app_token: wxfdksl
|
|
|
|
user_key: baa5fe97f2c5ab3ca8f0bb59
|
|
|
|
delegate_to: localhost
|
2020-09-03 08:01:52 +02:00
|
|
|
|
|
|
|
- name: Send notifications via pushover.net to a specific device
|
|
|
|
community.general.pushover:
|
|
|
|
msg: '{{ inventory_hostname }} has been lost somewhere'
|
|
|
|
app_token: wxfdksl
|
|
|
|
user_key: baa5fe97f2c5ab3ca8f0bb59
|
|
|
|
device: admins-iPhone
|
|
|
|
delegate_to: localhost
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class Pushover(object):
|
|
|
|
''' Instantiates a pushover object, use it to send notifications '''
|
|
|
|
base_uri = 'https://api.pushover.net'
|
|
|
|
|
|
|
|
def __init__(self, module, user, token):
|
|
|
|
self.module = module
|
|
|
|
self.user = user
|
|
|
|
self.token = token
|
|
|
|
|
2020-09-03 08:01:52 +02:00
|
|
|
def run(self, priority, msg, title, device):
|
2020-03-09 10:11:07 +01:00
|
|
|
''' Do, whatever it is, we do. '''
|
|
|
|
|
|
|
|
url = '%s/1/messages.json' % (self.base_uri)
|
|
|
|
|
|
|
|
# parse config
|
|
|
|
options = dict(user=self.user,
|
|
|
|
token=self.token,
|
|
|
|
priority=priority,
|
|
|
|
message=msg)
|
|
|
|
|
|
|
|
if title is not None:
|
|
|
|
options = dict(options,
|
|
|
|
title=title)
|
|
|
|
|
2020-09-03 08:01:52 +02:00
|
|
|
if device is not None:
|
|
|
|
options = dict(options,
|
|
|
|
device=device)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
data = urlencode(options)
|
|
|
|
|
|
|
|
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
|
|
|
r, info = fetch_url(self.module, url, method='POST', data=data, headers=headers)
|
|
|
|
if info['status'] != 200:
|
|
|
|
raise Exception(info)
|
|
|
|
|
|
|
|
return r.read()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
title=dict(type='str'),
|
|
|
|
msg=dict(required=True),
|
|
|
|
app_token=dict(required=True, no_log=True),
|
|
|
|
user_key=dict(required=True, no_log=True),
|
|
|
|
pri=dict(required=False, default='0', choices=['-2', '-1', '0', '1', '2']),
|
2020-09-03 08:01:52 +02:00
|
|
|
device=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
msg_object = Pushover(module, module.params['user_key'], module.params['app_token'])
|
|
|
|
try:
|
2020-09-03 08:01:52 +02:00
|
|
|
response = msg_object.run(module.params['pri'], module.params['msg'], module.params['title'], module.params['device'])
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception:
|
|
|
|
module.fail_json(msg='Unable to send msg via pushover')
|
|
|
|
|
|
|
|
module.exit_json(msg='message sent successfully: %s' % response, changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|