2014-10-19 23:50:55 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2012, Jim Richardson <weaselkeeper@gmail.com>
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
2014-10-19 23:55:46 +02:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2014-10-19 23:50:55 +02:00
|
|
|
#
|
2014-10-19 23:55:46 +02:00
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2014-10-19 23:50:55 +02:00
|
|
|
#
|
2014-10-19 23:55:46 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-19 23:50:55 +02:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: pushover
|
2015-03-26 04:15:37 +01:00
|
|
|
version_added: "2.0"
|
2016-02-08 05:20:32 +01:00
|
|
|
short_description: Send notifications via U(https://pushover.net)
|
2014-10-19 23:50:55 +02:00
|
|
|
description:
|
|
|
|
- Send notifications via pushover, to subscriber list of devices, and email
|
2015-03-29 22:51:30 +02:00
|
|
|
addresses. Requires pushover app on devices.
|
2014-10-19 23:50:55 +02:00
|
|
|
notes:
|
|
|
|
- You will require a pushover.net account to use this module. But no account
|
2015-03-29 22:51:30 +02:00
|
|
|
is required to receive messages.
|
2014-10-19 23:50:55 +02:00
|
|
|
options:
|
|
|
|
msg:
|
|
|
|
description:
|
2015-11-02 20:57:02 +01:00
|
|
|
- What message you wish to send.
|
2014-10-19 23:50:55 +02:00
|
|
|
required: true
|
|
|
|
app_token:
|
|
|
|
description:
|
2015-11-02 20:57:02 +01:00
|
|
|
- Pushover issued token identifying your pushover app.
|
2014-10-19 23:50:55 +02:00
|
|
|
required: true
|
|
|
|
user_key:
|
|
|
|
description:
|
2015-11-02 20:57:02 +01:00
|
|
|
- Pushover issued authentication key for your user.
|
2014-10-19 23:50:55 +02:00
|
|
|
required: true
|
|
|
|
pri:
|
2016-02-08 05:20:32 +01:00
|
|
|
description:
|
|
|
|
- Message priority (see U(https://pushover.net) for details.)
|
2014-10-19 23:50:55 +02:00
|
|
|
required: false
|
|
|
|
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Jim Richardson (@weaselkeeper)"
|
2014-10-19 23:50:55 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- local_action: pushover msg="{{inventory_hostname}} has exploded in flames,
|
|
|
|
It is now time to panic" app_token=wxfdksl user_key=baa5fe97f2c5ab3ca8f0bb59
|
|
|
|
'''
|
|
|
|
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
2015-07-28 21:23:54 +02:00
|
|
|
class Pushover(object):
|
2014-10-19 23:50:55 +02:00
|
|
|
''' Instantiates a pushover object, use it to send notifications '''
|
2015-07-28 21:23:54 +02:00
|
|
|
base_uri = 'https://api.pushover.net'
|
|
|
|
port = 443
|
2014-10-19 23:50:55 +02:00
|
|
|
|
2015-07-28 21:23:54 +02:00
|
|
|
def __init__(self, module, user, token):
|
|
|
|
self.module = module
|
|
|
|
self.user = user
|
|
|
|
self.token = token
|
2014-10-19 23:50:55 +02:00
|
|
|
|
2015-07-28 21:23:54 +02:00
|
|
|
def run(self, priority, msg):
|
2014-10-19 23:50:55 +02:00
|
|
|
''' Do, whatever it is, we do. '''
|
2015-07-28 21:23:54 +02:00
|
|
|
|
|
|
|
url = '%s:%s/1/messages.json' % (self.base_uri, self.port)
|
|
|
|
|
2014-10-19 23:50:55 +02:00
|
|
|
# parse config
|
2015-07-28 21:23:54 +02:00
|
|
|
options = dict(user=self.user,
|
|
|
|
token=self.token,
|
|
|
|
priority=priority,
|
|
|
|
message=msg)
|
|
|
|
data = urllib.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()
|
2014-10-19 23:50:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
msg=dict(required=True),
|
2016-04-13 20:32:47 +02:00
|
|
|
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']),
|
2014-10-19 23:50:55 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2015-07-28 21:23:54 +02:00
|
|
|
msg_object = Pushover(module, module.params['user_key'], module.params['app_token'])
|
2014-10-19 23:50:55 +02:00
|
|
|
try:
|
2015-10-19 18:14:51 +02:00
|
|
|
response = msg_object.run(module.params['pri'], module.params['msg'])
|
2014-10-19 23:50:55 +02:00
|
|
|
except:
|
2014-10-20 06:19:25 +02:00
|
|
|
module.fail_json(msg='Unable to send msg via pushover')
|
2014-10-19 23:50:55 +02:00
|
|
|
|
2015-10-19 18:14:51 +02:00
|
|
|
module.exit_json(msg='message sent successfully: %s' % response, changed=False)
|
2014-10-19 23:50:55 +02:00
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2015-07-28 21:23:54 +02:00
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|