| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2018, John Imison <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: rabbitmq_publish |
| 14 |
short_description: Publish a message to a RabbitMQ queue. |
| 15 |
description: |
| 16 |
- Publish a message on a RabbitMQ queue using a blocking connection. |
| 17 |
options: |
| 18 |
url: |
| 19 |
description: |
| 20 |
- An URL connection string to connect to the RabbitMQ server. |
| 21 |
- I(url) and I(host)/I(port)/I(user)/I(pass)/I(vhost) are mutually exclusive, use either or but not both. |
| 22 |
type: str |
| 23 |
proto: |
| 24 |
description: |
| 25 |
- The protocol to use. |
| 26 |
type: str |
| 27 |
choices: [amqps, amqp] |
| 28 |
host: |
| 29 |
description: |
| 30 |
- The RabbitMQ server hostname or IP. |
| 31 |
type: str |
| 32 |
port: |
| 33 |
description: |
| 34 |
- The RabbitMQ server port. |
| 35 |
type: int |
| 36 |
username: |
| 37 |
description: |
| 38 |
- The RabbitMQ username. |
| 39 |
type: str |
| 40 |
password: |
| 41 |
description: |
| 42 |
- The RabbitMQ password. |
| 43 |
type: str |
| 44 |
vhost: |
| 45 |
description: |
| 46 |
- The virtual host to target. |
| 47 |
- If default vhost is required, use C('%2F'). |
| 48 |
type: str |
| 49 |
queue: |
| 50 |
description: |
| 51 |
- The queue to publish a message to. If no queue is specified, RabbitMQ will return a random queue name. |
| 52 |
- A C(queue) cannot be provided if an C(exchange) is specified. |
| 53 |
type: str |
| 54 |
exchange: |
| 55 |
description: |
| 56 |
- The exchange to publish a message to. |
| 57 |
- An C(exchange) cannot be provided if a C(queue) is specified. |
| 58 |
type: str |
| 59 |
routing_key: |
| 60 |
description: |
| 61 |
- The routing key. |
| 62 |
type: str |
| 63 |
body: |
| 64 |
description: |
| 65 |
- The body of the message. |
| 66 |
- A C(body) cannot be provided if a C(src) is specified. |
| 67 |
type: str |
| 68 |
src: |
| 69 |
description: |
| 70 |
- A file to upload to the queue. Automatic mime type detection is attempted if content_type is not defined (left as default). |
| 71 |
- A C(src) cannot be provided if a C(body) is specified. |
| 72 |
- The filename is added to the headers of the posted message to RabbitMQ. Key being the C(filename), value is the filename. |
| 73 |
type: path |
| 74 |
aliases: ['file'] |
| 75 |
content_type: |
| 76 |
description: |
| 77 |
- The content type of the body. |
| 78 |
type: str |
| 79 |
default: text/plain |
| 80 |
durable: |
| 81 |
description: |
| 82 |
- Set the queue to be durable. |
| 83 |
type: bool |
| 84 |
default: False |
| 85 |
exclusive: |
| 86 |
description: |
| 87 |
- Set the queue to be exclusive. |
| 88 |
type: bool |
| 89 |
default: False |
| 90 |
auto_delete: |
| 91 |
description: |
| 92 |
- Set the queue to auto delete. |
| 93 |
type: bool |
| 94 |
default: False |
| 95 |
headers: |
| 96 |
description: |
| 97 |
- A dictionary of headers to post with the message. |
| 98 |
type: dict |
| 99 |
default: {} |
| 100 |
cafile: |
| 101 |
description: |
| 102 |
- CA file used during connection to the RabbitMQ server over SSL. |
| 103 |
- If this option is specified, also I(certfile) and I(keyfile) must be specified. |
| 104 |
type: str |
| 105 |
certfile: |
| 106 |
description: |
| 107 |
- Client certificate to establish SSL connection. |
| 108 |
- If this option is specified, also I(cafile) and I(keyfile) must be specified. |
| 109 |
type: str |
| 110 |
keyfile: |
| 111 |
description: |
| 112 |
- Client key to establish SSL connection. |
| 113 |
- If this option is specified, also I(cafile) and I(certfile) must be specified. |
| 114 |
type: str |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
requirements: [ pika ] |
| 119 |
notes: |
| 120 |
- This module requires the pika python library U(https://pika.readthedocs.io/). |
| 121 |
- Pika is a pure-Python implementation of the AMQP 0-9-1 protocol that tries to stay fairly independent of the underlying network support library. |
| 122 |
- This module is tested against RabbitMQ. Other AMQP 0.9.1 protocol based servers may work but not tested/guaranteed. |
| 123 |
- The certificate authentication was tested with certificates created |
| 124 |
via U(https://www.rabbitmq.com/ssl.html#automated-certificate-generation) and RabbitMQ |
| 125 |
configuration variables C(ssl_options.verify = verify_peer) & C(ssl_options.fail_if_no_peer_cert = true). |
| 126 |
author: "John Imison (@Im0)" |
| 127 |
''' |
| 128 |
|
| 129 |
EXAMPLES = r''' |
| 130 |
- name: Publish to an exchange |
| 131 |
community.rabbitmq.rabbitmq_publish: |
| 132 |
exchange: exchange1 |
| 133 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 134 |
body: "Hello exchange from ansible module rabbitmq_publish" |
| 135 |
content_type: "text/plain" |
| 136 |
|
| 137 |
- name: Publish to an exchange with routing_key |
| 138 |
community.rabbitmq.rabbitmq_publish: |
| 139 |
exchange: exchange1 |
| 140 |
routing_key: queue1 |
| 141 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 142 |
body: "Hello queue via exchange routing_key from ansible module rabbitmq_publish" |
| 143 |
content_type: "text/plain" |
| 144 |
|
| 145 |
- name: Publish a message to a queue with headers |
| 146 |
community.rabbitmq.rabbitmq_publish: |
| 147 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 148 |
queue: 'test' |
| 149 |
body: "Hello world from ansible module rabbitmq_publish" |
| 150 |
content_type: "text/plain" |
| 151 |
headers: |
| 152 |
myHeader: myHeaderValue |
| 153 |
|
| 154 |
- name: Publish a file to a queue |
| 155 |
community.rabbitmq.rabbitmq_publish: |
| 156 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 157 |
queue: 'images' |
| 158 |
file: 'path/to/logo.gif' |
| 159 |
|
| 160 |
- name: RabbitMQ auto generated queue |
| 161 |
community.rabbitmq.rabbitmq_publish: |
| 162 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 163 |
body: "Hello world random queue from ansible module rabbitmq_publish" |
| 164 |
content_type: "text/plain" |
| 165 |
|
| 166 |
- name: Publish with certs |
| 167 |
community.rabbitmq.rabbitmq_publish: |
| 168 |
url: "amqps://guest:[email protected]:5671/%2F" |
| 169 |
body: "Hello test queue from ansible module rabbitmq_publish via SSL certs" |
| 170 |
queue: 'test' |
| 171 |
content_type: "text/plain" |
| 172 |
cafile: 'ca_certificate.pem' |
| 173 |
certfile: 'client_certificate.pem' |
| 174 |
keyfile: 'client_key.pem' |
| 175 |
|
| 176 |
''' |
| 177 |
|
| 178 |
RETURN = r''' |
| 179 |
result: |
| 180 |
description: |
| 181 |
- If posted to an exchange, the result contains the status I(msg), content type I(content_type) the exchange name I(exchange) |
| 182 |
- and the routing key I(routing_key). |
| 183 |
- If posted to a queue, the result contains the status I(msg), content type I(content_type) and the queue name I(queue). |
| 184 |
returned: success |
| 185 |
type: dict |
| 186 |
sample: | |
| 187 |
'result': { 'content_type': 'text/plain', 'msg': 'Successfully published to queue test', 'queue': 'test' } |
| 188 |
''' |
| 189 |
|
| 190 |
from ansible.module_utils.basic import AnsibleModule |
| 191 |
from ansible_collections.community.rabbitmq.plugins.module_utils.rabbitmq import RabbitClient |
| 192 |
|
| 193 |
|
| 194 |
def main(): |
| 195 |
argument_spec = RabbitClient.rabbitmq_argument_spec() |
| 196 |
argument_spec.update( |
| 197 |
exchange=dict(type='str'), |
| 198 |
routing_key=dict(type='str', required=False, no_log=False), |
| 199 |
body=dict(type='str', required=False), |
| 200 |
src=dict(aliases=['file'], type='path', required=False), |
| 201 |
content_type=dict(default="text/plain", type='str'), |
| 202 |
durable=dict(default=False, type='bool'), |
| 203 |
exclusive=dict(default=False, type='bool'), |
| 204 |
auto_delete=dict(default=False, type='bool'), |
| 205 |
headers=dict(default={}, type='dict'), |
| 206 |
cafile=dict(type='str', required=False), |
| 207 |
certfile=dict(type='str', required=False), |
| 208 |
keyfile=dict(type='str', required=False, no_log=False), |
| 209 |
) |
| 210 |
module = AnsibleModule( |
| 211 |
argument_spec=argument_spec, |
| 212 |
mutually_exclusive=[['body', 'src'], ['queue', 'exchange']], |
| 213 |
required_together=[['cafile', 'certfile', 'keyfile']], |
| 214 |
supports_check_mode=False |
| 215 |
) |
| 216 |
|
| 217 |
rabbitmq = RabbitClient(module) |
| 218 |
|
| 219 |
if rabbitmq.basic_publish(): |
| 220 |
rabbitmq.close_connection() |
| 221 |
if (rabbitmq.queue is not None): |
| 222 |
module.exit_json(changed=True, result={"msg": "Successfully published to queue %s" % rabbitmq.queue, |
| 223 |
"queue": rabbitmq.queue, "content_type": rabbitmq.content_type} |
| 224 |
) |
| 225 |
elif (rabbitmq.exchange is not None): |
| 226 |
module.exit_json(changed=True, result={"msg": "Successfully published to exchange %s" % rabbitmq.exchange, |
| 227 |
"routing_key": rabbitmq.routing_key, "exchange": rabbitmq.exchange, "content_type": rabbitmq.content_type} |
| 228 |
) |
| 229 |
|
| 230 |
else: |
| 231 |
rabbitmq.close_connection() |
| 232 |
if (rabbitmq.queue is not None): |
| 233 |
module.fail_json(changed=False, msg="Unsuccessful publishing to queue %s" % rabbitmq.queue) |
| 234 |
elif (rabbitmq.exchange is not None): |
| 235 |
module.fail_json(changed=False, msg="Unsuccessful publishing to exchange %s" % rabbitmq.exchange) |
| 236 |
|
| 237 |
|
| 238 |
if __name__ == '__main__': |
| 239 |
main() |