2014-01-19 20:29:31 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-04-13 17:22:09 +02:00
|
|
|
# (c) 2015, Matt Makai <matthew.makai@gmail.com>
|
2014-01-19 20:29:31 +01: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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2014-03-16 21:45:38 +01:00
|
|
|
version_added: "1.6"
|
2014-03-21 19:26:20 +01:00
|
|
|
module: twilio
|
2014-01-23 21:55:23 +01:00
|
|
|
short_description: Sends a text message to a mobile phone through Twilio.
|
2014-01-19 20:29:31 +01:00
|
|
|
description:
|
2015-04-13 17:42:50 +02:00
|
|
|
- Sends a text message to a phone number through the Twilio messaging API.
|
2014-01-19 20:29:31 +01:00
|
|
|
notes:
|
2015-04-13 17:22:09 +02:00
|
|
|
- This module is non-idempotent because it sends an email through the
|
|
|
|
external API. It is idempotent only in the case that the module fails.
|
|
|
|
- Like the other notification modules, this one requires an external
|
2014-01-19 20:29:31 +01:00
|
|
|
dependency to work. In this case, you'll need a Twilio account with
|
2014-01-23 21:55:23 +01:00
|
|
|
a purchased or verified phone number to send the text message.
|
2014-01-19 20:29:31 +01:00
|
|
|
options:
|
|
|
|
account_sid:
|
|
|
|
description:
|
2015-04-13 17:22:09 +02:00
|
|
|
user's Twilio account token found on the account page
|
2014-01-19 20:29:31 +01:00
|
|
|
required: true
|
|
|
|
auth_token:
|
2015-04-13 17:22:09 +02:00
|
|
|
description: user's Twilio authentication token
|
2014-01-19 20:29:31 +01:00
|
|
|
required: true
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
the body of the text message
|
|
|
|
required: true
|
|
|
|
to_number:
|
|
|
|
description:
|
2015-04-13 17:22:09 +02:00
|
|
|
one or more phone numbers to send the text message to,
|
|
|
|
format +15551112222
|
2014-01-19 20:29:31 +01:00
|
|
|
required: true
|
|
|
|
from_number:
|
|
|
|
description:
|
2015-04-13 17:22:09 +02:00
|
|
|
the Twilio number to send the text message from, format +15551112222
|
2014-01-19 20:29:31 +01:00
|
|
|
required: true
|
2015-04-13 17:42:50 +02:00
|
|
|
media_url:
|
|
|
|
description:
|
|
|
|
a URL with a picture, video or sound clip to send with an MMS
|
|
|
|
(multimedia message) instead of a plain SMS
|
|
|
|
required: false
|
2015-04-13 17:22:09 +02:00
|
|
|
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Matt Makai (@makaimc)"
|
2014-01-19 20:29:31 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-04-13 17:22:09 +02:00
|
|
|
# send an SMS about the build status to (555) 303 5681
|
2015-04-13 17:42:50 +02:00
|
|
|
# note: replace account_sid and auth_token values with your credentials
|
|
|
|
# and you have to have the 'from_number' on your Twilio account
|
2015-04-13 17:22:09 +02:00
|
|
|
- twilio:
|
|
|
|
msg: "All servers with webserver role are now configured."
|
2015-04-13 17:42:50 +02:00
|
|
|
account_sid: "ACXXXXXXXXXXXXXXXXX"
|
|
|
|
auth_token: "ACXXXXXXXXXXXXXXXXX"
|
2015-04-13 17:22:09 +02:00
|
|
|
from_number: "+15552014545"
|
|
|
|
to_number: "+15553035681"
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
# send an SMS to multiple phone numbers about the deployment
|
2015-04-13 17:42:50 +02:00
|
|
|
# note: replace account_sid and auth_token values with your credentials
|
|
|
|
# and you have to have the 'from_number' on your Twilio account
|
2015-04-13 17:22:09 +02:00
|
|
|
- twilio:
|
|
|
|
msg: "This server's configuration is now complete."
|
2015-04-13 17:42:50 +02:00
|
|
|
account_sid: "ACXXXXXXXXXXXXXXXXX"
|
|
|
|
auth_token: "ACXXXXXXXXXXXXXXXXX"
|
2015-04-13 17:22:09 +02:00
|
|
|
from_number: "+15553258899"
|
|
|
|
to_number:
|
|
|
|
- "+15551113232"
|
|
|
|
- "+12025551235"
|
|
|
|
- "+19735559010"
|
|
|
|
delegate_to: localhost
|
|
|
|
|
2015-04-13 17:55:20 +02:00
|
|
|
# send an MMS to a single recipient with an update on the deployment
|
|
|
|
# and an image of the results
|
2015-04-13 17:42:50 +02:00
|
|
|
# note: replace account_sid and auth_token values with your credentials
|
|
|
|
# and you have to have the 'from_number' on your Twilio account
|
|
|
|
- twilio:
|
|
|
|
msg: "Deployment complete!"
|
|
|
|
account_sid: "ACXXXXXXXXXXXXXXXXX"
|
|
|
|
auth_token: "ACXXXXXXXXXXXXXXXXX"
|
|
|
|
from_number: "+15552014545"
|
|
|
|
to_number: "+15553035681"
|
|
|
|
media_url: "https://demo.twilio.com/logo.png"
|
|
|
|
delegate_to: localhost
|
2014-01-19 20:29:31 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
# =======================================
|
2015-04-13 17:22:09 +02:00
|
|
|
# twilio module support methods
|
2014-01-19 20:29:31 +01:00
|
|
|
#
|
|
|
|
try:
|
|
|
|
import urllib, urllib2
|
|
|
|
except ImportError:
|
|
|
|
module.fail_json(msg="urllib and urllib2 are required")
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
|
|
|
|
2015-04-13 17:22:09 +02:00
|
|
|
def post_twilio_api(module, account_sid, auth_token, msg, from_number,
|
2015-04-13 17:42:50 +02:00
|
|
|
to_number, media_url=None):
|
2014-01-23 21:55:23 +01:00
|
|
|
URI = "https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json" \
|
|
|
|
% (account_sid,)
|
2015-04-13 17:22:09 +02:00
|
|
|
AGENT = "Ansible"
|
2014-01-19 20:29:31 +01:00
|
|
|
|
2014-01-23 21:55:23 +01:00
|
|
|
data = {'From':from_number, 'To':to_number, 'Body':msg}
|
2015-04-13 17:42:50 +02:00
|
|
|
if media_url:
|
|
|
|
data['MediaUrl'] = media_url
|
2014-01-23 21:55:23 +01:00
|
|
|
encoded_data = urllib.urlencode(data)
|
|
|
|
request = urllib2.Request(URI)
|
|
|
|
base64string = base64.encodestring('%s:%s' % \
|
|
|
|
(account_sid, auth_token)).replace('\n', '')
|
|
|
|
request.add_header('User-Agent', AGENT)
|
|
|
|
request.add_header('Content-type', 'application/x-www-form-urlencoded')
|
2015-04-13 17:22:09 +02:00
|
|
|
request.add_header('Accept', 'application/json')
|
2014-01-23 21:55:23 +01:00
|
|
|
request.add_header('Authorization', 'Basic %s' % base64string)
|
|
|
|
return urllib2.urlopen(request, encoded_data)
|
2014-01-19 20:29:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
# =======================================
|
|
|
|
# Main
|
|
|
|
#
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
account_sid=dict(required=True),
|
|
|
|
auth_token=dict(required=True),
|
|
|
|
msg=dict(required=True),
|
|
|
|
from_number=dict(required=True),
|
|
|
|
to_number=dict(required=True),
|
2015-04-13 17:42:50 +02:00
|
|
|
media_url=dict(default=None, required=False),
|
2014-01-19 20:29:31 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
2015-04-13 17:22:09 +02:00
|
|
|
|
2014-01-19 20:29:31 +01:00
|
|
|
account_sid = module.params['account_sid']
|
|
|
|
auth_token = module.params['auth_token']
|
|
|
|
msg = module.params['msg']
|
|
|
|
from_number = module.params['from_number']
|
|
|
|
to_number = module.params['to_number']
|
2015-04-13 17:42:50 +02:00
|
|
|
media_url = module.params['media_url']
|
2014-01-19 20:29:31 +01:00
|
|
|
|
2015-04-13 18:06:27 +02:00
|
|
|
if not isinstance(to_number, list):
|
|
|
|
to_number = [to_number]
|
|
|
|
|
|
|
|
for number in to_number:
|
|
|
|
try:
|
2015-04-13 17:22:09 +02:00
|
|
|
post_twilio_api(module, account_sid, auth_token, msg,
|
2015-04-13 18:06:27 +02:00
|
|
|
from_number, number, media_url)
|
|
|
|
except Exception:
|
|
|
|
module.fail_json(msg="unable to send message to %s" % number)
|
2014-01-19 20:29:31 +01:00
|
|
|
|
2015-04-13 17:22:09 +02:00
|
|
|
module.exit_json(msg=msg, changed=False)
|
2014-01-19 20:29:31 +01:00
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|