-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpushover
executable file
·136 lines (121 loc) · 3.97 KB
/
pushover
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
import json
import sys
import urllib
DOCUMENTATION = """
module: pushover
description: Send notifications via Pushover (https://pushover.net)
author: Ricky Cook
requirements:
- pushover appliction token, and user key
options:
url:
description:
- The URL for the Pushover API
required: false
default: https://api.pushover.net/1/messages.json
token:
description:
- Your Pushover application token
required: true
user:
description:
- The key of the user to send the notification to
required: true
message:
description:
- Message to send in the notification
required: true
use_proxy:
description:
- if C(no), it will not use a proxy, even if one is defined in an
environment variable on the target hosts.
required: false
default: 'yes'
choices: ['yes', 'no']
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.
required: false
default: 'yes'
choices: ['yes', 'no']
timeout:
description:
- Timeout for URL request
required: false
default: 10
version_added: '1.8'
url_username:
description:
- The username for use in HTTP basic authentication. This parameter
can be used without C(url_password) for sites that allow empty
passwords.
required: false
version_added: '1.6'
url_password:
description:
- The password for use in HTTP basic authentication. If the
C(url_username) parameter is not specified, the C(url_password)
parameter will not be used.
required: false
version_added: '1.6'
"""
def main():
argument_spec = url_argument_spec()
argument_spec.update({
'url': dict(required=False,
default='https://api.pushover.net/1/messages.json'),
'timeout': dict(required=False,
type='int',
default=10),
'token': dict(required=True),
'user': dict(required=True),
'message': dict(required=True),
})
del argument_spec['force'] # always forced
module = AnsibleModule(argument_spec=argument_spec)
# Generate the POST data from a subset of the params
data = urllib.urlencode({
key: module.params[key]
for key in ('token', 'user', 'message')
})
rsp, info = fetch_url(
module,
module.params['url'],
method='POST',
force=True,
data=data,
use_proxy=module.params['use_proxy'],
timeout=module.params['timeout'],
)
# Check for success/fail response
if info['status'] != 200:
module.fail_json(msg="Request failed",
status_code=info['status'],
response=info['msg'],
url=module.params['url'],)
try:
obj = json.load(rsp)
except ValueError:
module.fail_json(msg="Unexpected non-JSON data from server",
mime_type=info['type'],
url=module.params['url'])
if obj['status'] != 1:
if 'errors' in obj and obj['errors']:
error_message = "Pushover failed to send notifications:\n"
error_message += "\n".join(
(" - %s" % message for message in obj['errors'])
)
module.fail_json(msg=error_message, **obj)
else:
module.fail_json(
msg="Pushover reported failure but gave no errors",
**obj
)
module.exit_json(url=module.params['url'], changed=True)
# Import Ansible and run
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()