| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2012, Jim Richardson <[email protected]> |
| 3 |
# Copyright (c) 2019, Bernd Arnold <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: pushover |
| 11 |
short_description: Send notifications through U(https://pushover.net) |
| 12 |
description: |
| 13 |
- Send notifications through pushover to subscriber list of devices and email addresses. Requires pushover app on devices. |
| 14 |
notes: |
| 15 |
- You need a pushover.net account to use this module. But no account is required to receive messages. |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
msg: |
| 25 |
type: str |
| 26 |
description: |
| 27 |
- What message you wish to send. |
| 28 |
required: true |
| 29 |
app_token: |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- Pushover issued token identifying your pushover app. |
| 33 |
required: true |
| 34 |
user_key: |
| 35 |
type: str |
| 36 |
description: |
| 37 |
- Pushover issued authentication key for your user. |
| 38 |
required: true |
| 39 |
title: |
| 40 |
type: str |
| 41 |
description: |
| 42 |
- Message title. |
| 43 |
pri: |
| 44 |
type: str |
| 45 |
description: |
| 46 |
- Message priority (see U(https://pushover.net) for details). |
| 47 |
default: '0' |
| 48 |
choices: ['-2', '-1', '0', '1', '2'] |
| 49 |
device: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- A device the message should be sent to. Multiple devices can be specified, separated by a comma. |
| 53 |
version_added: 1.2.0 |
| 54 |
|
| 55 |
author: |
| 56 |
- "Jim Richardson (@weaselkeeper)" |
| 57 |
- "Bernd Arnold (@wopfel)" |
| 58 |
""" |
| 59 |
|
| 60 |
EXAMPLES = r""" |
| 61 |
- name: Send notifications via pushover.net |
| 62 |
community.general.pushover: |
| 63 |
msg: '{{ inventory_hostname }} is acting strange ...' |
| 64 |
app_token: wxfdksl |
| 65 |
user_key: baa5fe97f2c5ab3ca8f0bb59 |
| 66 |
delegate_to: localhost |
| 67 |
|
| 68 |
- name: Send notifications via pushover.net |
| 69 |
community.general.pushover: |
| 70 |
title: 'Alert!' |
| 71 |
msg: '{{ inventory_hostname }} has exploded in flames, It is now time to panic' |
| 72 |
pri: 1 |
| 73 |
app_token: wxfdksl |
| 74 |
user_key: baa5fe97f2c5ab3ca8f0bb59 |
| 75 |
delegate_to: localhost |
| 76 |
|
| 77 |
- name: Send notifications via pushover.net to a specific device |
| 78 |
community.general.pushover: |
| 79 |
msg: '{{ inventory_hostname }} has been lost somewhere' |
| 80 |
app_token: wxfdksl |
| 81 |
user_key: baa5fe97f2c5ab3ca8f0bb59 |
| 82 |
device: admins-iPhone |
| 83 |
delegate_to: localhost |
| 84 |
""" |
| 85 |
|
| 86 |
from urllib.parse import urlencode |
| 87 |
|
| 88 |
from ansible.module_utils.basic import AnsibleModule |
| 89 |
from ansible.module_utils.urls import fetch_url |
| 90 |
|
| 91 |
|
| 92 |
class Pushover: |
| 93 |
"""Instantiates a pushover object, use it to send notifications""" |
| 94 |
|
| 95 |
base_uri = "https://api.pushover.net" |
| 96 |
|
| 97 |
def __init__(self, module, user, token): |
| 98 |
self.module = module |
| 99 |
self.user = user |
| 100 |
self.token = token |
| 101 |
|
| 102 |
def run(self, priority, msg, title, device): |
| 103 |
"""Do, whatever it is, we do.""" |
| 104 |
|
| 105 |
url = f"{self.base_uri}/1/messages.json" |
| 106 |
|
| 107 |
# parse config |
| 108 |
options = dict(user=self.user, token=self.token, priority=priority, message=msg) |
| 109 |
|
| 110 |
if title is not None: |
| 111 |
options = dict(options, title=title) |
| 112 |
|
| 113 |
if device is not None: |
| 114 |
options = dict(options, device=device) |
| 115 |
|
| 116 |
data = urlencode(options) |
| 117 |
|
| 118 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 119 |
r, info = fetch_url(self.module, url, method="POST", data=data, headers=headers) |
| 120 |
if info["status"] != 200: |
| 121 |
raise Exception(info) |
| 122 |
|
| 123 |
return r.read() |
| 124 |
|
| 125 |
|
| 126 |
def main(): |
| 127 |
module = AnsibleModule( |
| 128 |
argument_spec=dict( |
| 129 |
title=dict(type="str"), |
| 130 |
msg=dict(required=True), |
| 131 |
app_token=dict(required=True, no_log=True), |
| 132 |
user_key=dict(required=True, no_log=True), |
| 133 |
pri=dict(default="0", choices=["-2", "-1", "0", "1", "2"]), |
| 134 |
device=dict(type="str"), |
| 135 |
), |
| 136 |
) |
| 137 |
|
| 138 |
msg_object = Pushover(module, module.params["user_key"], module.params["app_token"]) |
| 139 |
try: |
| 140 |
response = msg_object.run( |
| 141 |
module.params["pri"], module.params["msg"], module.params["title"], module.params["device"] |
| 142 |
) |
| 143 |
except Exception: |
| 144 |
module.fail_json(msg="Unable to send msg via pushover") |
| 145 |
|
| 146 |
module.exit_json(msg=f"message sent successfully: {response}", changed=False) |
| 147 |
|
| 148 |
|
| 149 |
if __name__ == "__main__": |
| 150 |
main() |