2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Michael J. Schultz <mjschultz@gmail.com>
|
2017-08-12 16:27:11 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
module: sns
|
2018-10-07 20:03:48 +00:00
|
|
|
short_description: Send Amazon Simple Notification Service messages
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Sends a notification to a topic on your Amazon SNS account.
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: 1.6
|
2018-10-07 20:03:48 +00:00
|
|
|
author:
|
|
|
|
- Michael J. Schultz (@mjschultz)
|
|
|
|
- Paul Arthur (@flowerysong)
|
2014-09-26 01:01:01 +00:00
|
|
|
options:
|
|
|
|
msg:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Default message for subscriptions without a more specific message.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
|
|
|
aliases: [ "default" ]
|
|
|
|
subject:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message subject
|
2014-09-26 01:01:01 +00:00
|
|
|
topic:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- The name or ARN of the topic to publish to.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
|
|
|
email:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to email subscriptions.
|
|
|
|
email_json:
|
|
|
|
description:
|
|
|
|
- Message to send to email-json subscriptions
|
|
|
|
version_added: '2.8'
|
2014-09-26 01:01:01 +00:00
|
|
|
sqs:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to SQS subscriptions
|
2014-09-26 01:01:01 +00:00
|
|
|
sms:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to SMS subscriptions
|
2014-09-26 01:01:01 +00:00
|
|
|
http:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to HTTP subscriptions
|
2014-09-26 01:01:01 +00:00
|
|
|
https:
|
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to HTTPS subscriptions
|
|
|
|
application:
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to application subscriptions
|
|
|
|
version_added: '2.8'
|
|
|
|
lambda:
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
2018-10-07 20:03:48 +00:00
|
|
|
- Message to send to Lambda subscriptions
|
|
|
|
version_added: '2.8'
|
2017-01-24 16:09:21 +00:00
|
|
|
message_attributes:
|
|
|
|
description:
|
|
|
|
- Dictionary of message attributes. These are optional structured data entries to be sent along to the endpoint.
|
|
|
|
- This is in AWS's distinct Name/Type/Value format; see example below.
|
|
|
|
message_structure:
|
|
|
|
description:
|
|
|
|
- The payload format to use for the message.
|
2018-10-07 20:03:48 +00:00
|
|
|
- This must be 'json' to support protocol-specific messages (`http`, `https`, `email`, `sms`, `sqs`). It must be 'string' to support message_attributes.
|
2017-01-24 16:09:21 +00:00
|
|
|
default: json
|
|
|
|
choices: ['json', 'string']
|
2018-01-16 12:18:51 +00:00
|
|
|
extends_documentation_fragment:
|
2018-10-07 20:03:48 +00:00
|
|
|
- ec2
|
|
|
|
- aws
|
2016-08-24 15:40:31 +00:00
|
|
|
requirements:
|
2018-10-07 20:03:48 +00:00
|
|
|
- boto3
|
|
|
|
- botocore
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: Send default notification message via SNS
|
2016-12-11 22:14:00 +00:00
|
|
|
sns:
|
|
|
|
msg: '{{ inventory_hostname }} has completed the play.'
|
|
|
|
subject: Deploy complete!
|
|
|
|
topic: deploy
|
|
|
|
delegate_to: localhost
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
- name: Send notification messages via SNS with short message for SMS
|
2016-12-11 22:14:00 +00:00
|
|
|
sns:
|
|
|
|
msg: '{{ inventory_hostname }} has completed the play.'
|
|
|
|
sms: deployed!
|
|
|
|
subject: Deploy complete!
|
|
|
|
topic: deploy
|
|
|
|
delegate_to: localhost
|
2017-01-24 16:09:21 +00:00
|
|
|
|
|
|
|
- name: Send message with message_attributes
|
|
|
|
sns:
|
|
|
|
topic: "deploy"
|
|
|
|
msg: "message with extra details!"
|
|
|
|
message_attributes:
|
|
|
|
channel:
|
|
|
|
data_type: String
|
|
|
|
string_value: "mychannel"
|
|
|
|
color:
|
|
|
|
data_type: String
|
|
|
|
string_value: "green"
|
|
|
|
delegate_to: localhost
|
2014-09-26 01:01:01 +00:00
|
|
|
"""
|
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
RETURN = """
|
|
|
|
msg:
|
|
|
|
description: Human-readable diagnostic information
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
sample: OK
|
|
|
|
message_id:
|
|
|
|
description: The message ID of the submitted message
|
|
|
|
returned: when success
|
|
|
|
type: string
|
|
|
|
sample: 2f681ef0-6d76-5c94-99b2-4ae3996ce57b
|
|
|
|
"""
|
|
|
|
|
2017-07-29 10:24:30 +00:00
|
|
|
import json
|
|
|
|
import traceback
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
try:
|
2018-10-07 20:03:48 +00:00
|
|
|
from botocore.exceptions import BotoCoreError, ClientError
|
2014-09-26 01:01:01 +00:00
|
|
|
except ImportError:
|
2018-10-07 20:03:48 +00:00
|
|
|
pass # Handled by AnsibleAWSModule
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
from ansible.module_utils.aws.core import AnsibleAWSModule
|
2017-07-29 10:24:30 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
def arn_topic_lookup(module, client, short_topic):
|
2014-09-26 01:01:01 +00:00
|
|
|
lookup_topic = ':{}'.format(short_topic)
|
2018-10-07 20:03:48 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
paginator = client.get_paginator('list_topics')
|
|
|
|
topic_iterator = paginator.paginate()
|
|
|
|
for response in topic_iterator:
|
|
|
|
for topic in response['Topics']:
|
|
|
|
if topic['TopicArn'].endswith(lookup_topic):
|
|
|
|
return topic['TopicArn']
|
|
|
|
except (BotoCoreError, ClientError) as e:
|
|
|
|
module.fail_json_aws(e, msg='Failed to look up topic ARN')
|
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-10-07 20:03:48 +00:00
|
|
|
protocols = [
|
|
|
|
'http',
|
|
|
|
'https',
|
|
|
|
'email',
|
|
|
|
'email_json',
|
|
|
|
'sms',
|
|
|
|
'sqs',
|
|
|
|
'application',
|
|
|
|
'lambda',
|
|
|
|
]
|
|
|
|
|
|
|
|
argument_spec = dict(
|
|
|
|
msg=dict(required=True, aliases=['default']),
|
|
|
|
subject=dict(),
|
|
|
|
topic=dict(required=True),
|
|
|
|
message_attributes=dict(type='dict'),
|
|
|
|
message_structure=dict(choices=['json', 'string'], default='json'),
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
for p in protocols:
|
|
|
|
argument_spec[p] = dict()
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
module = AnsibleAWSModule(argument_spec=argument_spec)
|
|
|
|
|
|
|
|
sns_kwargs = dict(
|
|
|
|
Message=module.params['msg'],
|
|
|
|
Subject=module.params['subject'],
|
|
|
|
MessageStructure=module.params['message_structure'],
|
|
|
|
)
|
|
|
|
|
|
|
|
if module.params['message_attributes']:
|
|
|
|
if module.params['message_structure'] != 'string':
|
|
|
|
module.fail_json(msg='message_attributes is only supported when the message_structure is "string".')
|
|
|
|
sns_kwargs['MessageAttributes'] = module.params['message_attributes']
|
|
|
|
|
|
|
|
dict_msg = {
|
|
|
|
'default': sns_kwargs['Message']
|
|
|
|
}
|
|
|
|
|
|
|
|
for p in protocols:
|
|
|
|
if module.params[p]:
|
|
|
|
if sns_kwargs['MessageStructure'] != 'json':
|
|
|
|
module.fail_json(msg='Protocol-specific messages are only supported when message_structure is "json".')
|
|
|
|
dict_msg[p.replace('_', '-')] = module.params[p]
|
|
|
|
|
|
|
|
client = module.client('sns')
|
2015-09-30 20:50:44 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
topic = module.params['topic']
|
|
|
|
if ':' in topic:
|
2018-10-07 20:03:48 +00:00
|
|
|
# Short names can't contain ':' so we'll assume this is the full ARN
|
|
|
|
sns_kwargs['TopicArn'] = topic
|
2014-09-26 01:01:01 +00:00
|
|
|
else:
|
2018-10-07 20:03:48 +00:00
|
|
|
sns_kwargs['TopicArn'] = arn_topic_lookup(module, client, topic)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
if not sns_kwargs['TopicArn']:
|
2014-09-26 01:01:01 +00:00
|
|
|
module.fail_json(msg='Could not find topic: {}'.format(topic))
|
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
if sns_kwargs['MessageStructure'] == 'json':
|
|
|
|
sns_kwargs['Message'] = json.dumps(dict_msg)
|
2017-01-24 16:09:21 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
2018-10-07 20:03:48 +00:00
|
|
|
result = client.publish(**sns_kwargs)
|
|
|
|
except (BotoCoreError, ClientError) as e:
|
|
|
|
module.fail_json_aws(e, msg='Failed to publish message')
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2018-10-07 20:03:48 +00:00
|
|
|
module.exit_json(msg='OK', message_id=result['MessageId'])
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-07-29 10:24:30 +00:00
|
|
|
|
2016-08-24 15:40:31 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|