| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2014, Michael J. Schultz <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
module: sns |
| 9 |
short_description: Send Amazon Simple Notification Service messages |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Sends a notification to a topic on your Amazon SNS account. |
| 13 |
author: |
| 14 |
- Michael J. Schultz (@mjschultz) |
| 15 |
- Paul Arthur (@flowerysong) |
| 16 |
options: |
| 17 |
msg: |
| 18 |
description: |
| 19 |
- Default message for subscriptions without a more specific message. |
| 20 |
required: true |
| 21 |
aliases: [ "default" ] |
| 22 |
type: str |
| 23 |
subject: |
| 24 |
description: |
| 25 |
- Message subject |
| 26 |
type: str |
| 27 |
topic: |
| 28 |
description: |
| 29 |
- The name or ARN of the topic to publish to. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
email: |
| 33 |
description: |
| 34 |
- Message to send to email subscriptions. |
| 35 |
type: str |
| 36 |
email_json: |
| 37 |
description: |
| 38 |
- Message to send to email-json subscriptions. |
| 39 |
type: str |
| 40 |
sqs: |
| 41 |
description: |
| 42 |
- Message to send to SQS subscriptions. |
| 43 |
type: str |
| 44 |
sms: |
| 45 |
description: |
| 46 |
- Message to send to SMS subscriptions. |
| 47 |
type: str |
| 48 |
http: |
| 49 |
description: |
| 50 |
- Message to send to HTTP subscriptions. |
| 51 |
type: str |
| 52 |
https: |
| 53 |
description: |
| 54 |
- Message to send to HTTPS subscriptions. |
| 55 |
type: str |
| 56 |
application: |
| 57 |
description: |
| 58 |
- Message to send to application subscriptions. |
| 59 |
type: str |
| 60 |
lambda: |
| 61 |
description: |
| 62 |
- Message to send to Lambda subscriptions. |
| 63 |
type: str |
| 64 |
message_attributes: |
| 65 |
description: |
| 66 |
- Dictionary of message attributes. These are optional structured data entries to be sent along to the endpoint. |
| 67 |
- This is in AWS's distinct Name/Type/Value format; see example below. |
| 68 |
type: dict |
| 69 |
message_structure: |
| 70 |
description: |
| 71 |
- The payload format to use for the message. |
| 72 |
- This must be C(json) to support protocol-specific messages (C(http), C(https), C(email), C(sms), C(sqs)). |
| 73 |
- It must be C(string) to support I(message_attributes). |
| 74 |
default: json |
| 75 |
choices: ['json', 'string'] |
| 76 |
type: str |
| 77 |
message_group_id: |
| 78 |
description: |
| 79 |
- A tag which is used to process messages that belong to the same group in a FIFO manner. |
| 80 |
- Has to be included when publishing a message to a fifo topic. |
| 81 |
- Can contain up to 128 alphanumeric characters and punctuation. |
| 82 |
type: str |
| 83 |
version_added: 5.4.0 |
| 84 |
message_deduplication_id: |
| 85 |
description: |
| 86 |
- Only in connection with the message_group_id. |
| 87 |
- Overwrites the auto generated MessageDeduplicationId. |
| 88 |
- Can contain up to 128 alphanumeric characters and punctuation. |
| 89 |
- Messages with the same deduplication id getting recognized as the same message. |
| 90 |
- Gets overwritten by an auto generated token, if the topic has ContentBasedDeduplication set. |
| 91 |
type: str |
| 92 |
version_added: 5.4.0 |
| 93 |
|
| 94 |
extends_documentation_fragment: |
| 95 |
- amazon.aws.region.modules |
| 96 |
- amazon.aws.common.modules |
| 97 |
- amazon.aws.boto3 |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = r""" |
| 101 |
- name: Send default notification message via SNS |
| 102 |
community.aws.sns: |
| 103 |
msg: '{{ inventory_hostname }} has completed the play.' |
| 104 |
subject: Deploy complete! |
| 105 |
topic: deploy |
| 106 |
delegate_to: localhost |
| 107 |
|
| 108 |
- name: Send notification messages via SNS with short message for SMS |
| 109 |
community.aws.sns: |
| 110 |
msg: '{{ inventory_hostname }} has completed the play.' |
| 111 |
sms: deployed! |
| 112 |
subject: Deploy complete! |
| 113 |
topic: deploy |
| 114 |
delegate_to: localhost |
| 115 |
|
| 116 |
- name: Send message with message_attributes |
| 117 |
community.aws.sns: |
| 118 |
topic: "deploy" |
| 119 |
msg: "message with extra details!" |
| 120 |
message_attributes: |
| 121 |
channel: |
| 122 |
data_type: String |
| 123 |
string_value: "mychannel" |
| 124 |
color: |
| 125 |
data_type: String |
| 126 |
string_value: "green" |
| 127 |
delegate_to: localhost |
| 128 |
|
| 129 |
- name: Send message to a fifo topic |
| 130 |
community.aws.sns: |
| 131 |
topic: "deploy" |
| 132 |
msg: "Message with message group id" |
| 133 |
subject: Deploy complete! |
| 134 |
message_group_id: "deploy-1" |
| 135 |
delegate_to: localhost |
| 136 |
""" |
| 137 |
|
| 138 |
RETURN = r""" |
| 139 |
msg: |
| 140 |
description: Human-readable diagnostic information |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: OK |
| 144 |
message_id: |
| 145 |
description: The message ID of the submitted message |
| 146 |
returned: when success |
| 147 |
type: str |
| 148 |
sample: 2f681ef0-6d76-5c94-99b2-4ae3996ce57b |
| 149 |
sequence_number: |
| 150 |
description: A 128 bits long sequence number which gets assigned to the message in fifo topics |
| 151 |
returned: when success |
| 152 |
type: str |
| 153 |
""" |
| 154 |
|
| 155 |
import json |
| 156 |
|
| 157 |
try: |
| 158 |
from botocore.exceptions import BotoCoreError |
| 159 |
from botocore.exceptions import ClientError |
| 160 |
except ImportError: |
| 161 |
pass # Handled by AnsibleAWSModule |
| 162 |
|
| 163 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 164 |
from ansible_collections.community.aws.plugins.module_utils.sns import topic_arn_lookup |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
protocols = [ |
| 169 |
"http", |
| 170 |
"https", |
| 171 |
"email", |
| 172 |
"email_json", |
| 173 |
"sms", |
| 174 |
"sqs", |
| 175 |
"application", |
| 176 |
"lambda", |
| 177 |
] |
| 178 |
|
| 179 |
argument_spec = dict( |
| 180 |
msg=dict(required=True, aliases=["default"]), |
| 181 |
subject=dict(), |
| 182 |
topic=dict(required=True), |
| 183 |
message_attributes=dict(type="dict"), |
| 184 |
message_structure=dict(choices=["json", "string"], default="json"), |
| 185 |
message_group_id=dict(), |
| 186 |
message_deduplication_id=dict(), |
| 187 |
) |
| 188 |
|
| 189 |
for p in protocols: |
| 190 |
argument_spec[p] = dict() |
| 191 |
|
| 192 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 193 |
|
| 194 |
sns_kwargs = dict( |
| 195 |
Message=module.params["msg"], |
| 196 |
Subject=module.params["subject"], |
| 197 |
MessageStructure=module.params["message_structure"], |
| 198 |
) |
| 199 |
|
| 200 |
if module.params["message_attributes"]: |
| 201 |
if module.params["message_structure"] != "string": |
| 202 |
module.fail_json(msg='message_attributes is only supported when the message_structure is "string".') |
| 203 |
sns_kwargs["MessageAttributes"] = module.params["message_attributes"] |
| 204 |
|
| 205 |
if module.params["message_group_id"]: |
| 206 |
sns_kwargs["MessageGroupId"] = module.params["message_group_id"] |
| 207 |
if module.params["message_deduplication_id"]: |
| 208 |
sns_kwargs["MessageDeduplicationId"] = module.params["message_deduplication_id"] |
| 209 |
|
| 210 |
dict_msg = {"default": sns_kwargs["Message"]} |
| 211 |
|
| 212 |
for p in protocols: |
| 213 |
if module.params[p]: |
| 214 |
if sns_kwargs["MessageStructure"] != "json": |
| 215 |
module.fail_json(msg='Protocol-specific messages are only supported when message_structure is "json".') |
| 216 |
dict_msg[p.replace("_", "-")] = module.params[p] |
| 217 |
|
| 218 |
client = module.client("sns") |
| 219 |
|
| 220 |
topic = module.params["topic"] |
| 221 |
if ":" in topic: |
| 222 |
# Short names can't contain ':' so we'll assume this is the full ARN |
| 223 |
sns_kwargs["TopicArn"] = topic |
| 224 |
else: |
| 225 |
sns_kwargs["TopicArn"] = topic_arn_lookup(client, module, topic) |
| 226 |
|
| 227 |
if not sns_kwargs["TopicArn"]: |
| 228 |
module.fail_json(msg=f"Could not find topic: {topic}") |
| 229 |
|
| 230 |
if sns_kwargs["MessageStructure"] == "json": |
| 231 |
sns_kwargs["Message"] = json.dumps(dict_msg) |
| 232 |
|
| 233 |
try: |
| 234 |
result = client.publish(**sns_kwargs) |
| 235 |
except (BotoCoreError, ClientError) as e: |
| 236 |
module.fail_json_aws(e, msg="Failed to publish message") |
| 237 |
|
| 238 |
sns_result = dict(msg="OK", message_id=result["MessageId"]) |
| 239 |
|
| 240 |
if module.params["message_group_id"]: |
| 241 |
sns_result["sequence_number"] = result["SequenceNumber"] |
| 242 |
|
| 243 |
module.exit_json(**sns_result) |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == "__main__": |
| 247 |
main() |