2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2014, Matt Martz <matt@sivel.net>
|
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: nexmo
|
|
|
|
short_description: Send a SMS via nexmo
|
|
|
|
description:
|
|
|
|
- Send a SMS message via nexmo
|
|
|
|
author: "Matt Martz (@sivel)"
|
2023-02-20 17:30:26 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
api_key:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Nexmo API Key
|
|
|
|
required: true
|
|
|
|
api_secret:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Nexmo API Secret
|
|
|
|
required: true
|
|
|
|
src:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Nexmo Number to send from
|
|
|
|
required: true
|
|
|
|
dest:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: list
|
2021-02-16 07:11:37 +01:00
|
|
|
elements: int
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Phone number(s) to send SMS message to
|
|
|
|
required: true
|
|
|
|
msg:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Message to text to send. Messages longer than 160 characters will be
|
|
|
|
split into multiple messages
|
|
|
|
required: true
|
|
|
|
validate_certs:
|
|
|
|
description:
|
2022-08-24 19:59:46 +02:00
|
|
|
- If C(false), SSL certificates will not be validated. This should only be used
|
2020-03-09 10:11:07 +01:00
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
type: bool
|
2022-08-24 19:59:46 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
extends_documentation_fragment:
|
2023-02-20 17:30:26 +01:00
|
|
|
- ansible.builtin.url
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Send notification message via Nexmo
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.nexmo:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_key: 640c8a53
|
|
|
|
api_secret: 0ce239a6
|
|
|
|
src: 12345678901
|
|
|
|
dest:
|
|
|
|
- 10987654321
|
|
|
|
- 16789012345
|
|
|
|
msg: '{{ inventory_hostname }} completed'
|
|
|
|
delegate_to: localhost
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import fetch_url, url_argument_spec
|
|
|
|
|
|
|
|
|
|
|
|
NEXMO_API = 'https://rest.nexmo.com/sms/json'
|
|
|
|
|
|
|
|
|
|
|
|
def send_msg(module):
|
|
|
|
failed = list()
|
|
|
|
responses = dict()
|
|
|
|
msg = {
|
|
|
|
'api_key': module.params.get('api_key'),
|
|
|
|
'api_secret': module.params.get('api_secret'),
|
|
|
|
'from': module.params.get('src'),
|
|
|
|
'text': module.params.get('msg')
|
|
|
|
}
|
|
|
|
for number in module.params.get('dest'):
|
|
|
|
msg['to'] = number
|
|
|
|
url = "%s?%s" % (NEXMO_API, urlencode(msg))
|
|
|
|
|
|
|
|
headers = dict(Accept='application/json')
|
|
|
|
response, info = fetch_url(module, url, headers=headers)
|
|
|
|
if info['status'] != 200:
|
|
|
|
failed.append(number)
|
|
|
|
responses[number] = dict(failed=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
responses[number] = json.load(response)
|
|
|
|
except Exception:
|
|
|
|
failed.append(number)
|
|
|
|
responses[number] = dict(failed=True)
|
|
|
|
else:
|
|
|
|
for message in responses[number]['messages']:
|
|
|
|
if int(message['status']) != 0:
|
|
|
|
failed.append(number)
|
|
|
|
responses[number] = dict(failed=True, **responses[number])
|
|
|
|
|
|
|
|
if failed:
|
|
|
|
msg = 'One or messages failed to send'
|
|
|
|
else:
|
|
|
|
msg = ''
|
|
|
|
|
|
|
|
module.exit_json(failed=bool(failed), msg=msg, changed=False,
|
|
|
|
responses=responses)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = url_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
|
|
|
api_key=dict(required=True, no_log=True),
|
|
|
|
api_secret=dict(required=True, no_log=True),
|
|
|
|
src=dict(required=True, type='int'),
|
2021-02-16 07:11:37 +01:00
|
|
|
dest=dict(required=True, type='list', elements='int'),
|
2020-03-09 10:11:07 +01:00
|
|
|
msg=dict(required=True),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec
|
|
|
|
)
|
|
|
|
|
|
|
|
send_msg(module)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|