| 1 |
- name: Install requests and pika |
| 2 |
pip: |
| 3 |
name: requests,pika==1.3.0 |
| 4 |
state: present |
| 5 |
delegate_to: localhost |
| 6 |
|
| 7 |
- name: RabbitMQ basic publish test |
| 8 |
rabbitmq_publish: |
| 9 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 10 |
queue: 'publish_test' |
| 11 |
body: "Hello world from ansible module rabbitmq_publish" |
| 12 |
content_type: "text/plain" |
| 13 |
register: rabbit_basic_output1 |
| 14 |
delegate_to: localhost |
| 15 |
|
| 16 |
- assert: |
| 17 |
that: |
| 18 |
- "rabbit_basic_output1 is not failed" |
| 19 |
- "'publish_test' in rabbit_basic_output1.result.msg" |
| 20 |
- "'publish_test' in rabbit_basic_output1.result.queue" |
| 21 |
- "'text/plain' in rabbit_basic_output1.result.content_type" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
- name: Publish to random queue |
| 26 |
rabbitmq_publish: |
| 27 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 28 |
body: "RANDOM QUEUE POST" |
| 29 |
content_type: "text/plain" |
| 30 |
register: rabbit_random_queue_output |
| 31 |
delegate_to: localhost |
| 32 |
|
| 33 |
- assert: |
| 34 |
that: |
| 35 |
- "rabbit_random_queue_output is not failed" |
| 36 |
- "'amq.gen' in rabbit_random_queue_output.result.msg" |
| 37 |
- "'amq.gen' in rabbit_random_queue_output.result.queue" |
| 38 |
- "'text/plain' in rabbit_random_queue_output.result.content_type" |
| 39 |
|
| 40 |
- name: Copy binary to remote |
| 41 |
copy: |
| 42 |
src: "{{ role_path }}/files/image.gif" |
| 43 |
dest: "{{ remote_tmp_dir }}/image.gif" |
| 44 |
delegate_to: localhost |
| 45 |
|
| 46 |
- name: Publish binary to a queue |
| 47 |
rabbitmq_publish: |
| 48 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 49 |
queue: publish_test |
| 50 |
src: "{{ remote_tmp_dir }}/image.gif" |
| 51 |
register: rabbitmq_publish_file |
| 52 |
delegate_to: localhost |
| 53 |
|
| 54 |
- assert: |
| 55 |
that: |
| 56 |
- "rabbitmq_publish_file is not failed" |
| 57 |
- "'publish_test' in rabbitmq_publish_file.result.queue" |
| 58 |
- "'image/gif' in rabbitmq_publish_file.result.content_type" |
| 59 |
|
| 60 |
- name: Raise error for src and body defined |
| 61 |
rabbitmq_publish: |
| 62 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 63 |
queue: 'publish_test' |
| 64 |
src: "{{ remote_tmp_dir }}/image.gif" |
| 65 |
body: blah |
| 66 |
register: rabbit_basic_fail_output1 |
| 67 |
ignore_errors: yes |
| 68 |
delegate_to: localhost |
| 69 |
|
| 70 |
- assert: |
| 71 |
that: |
| 72 |
- "rabbit_basic_fail_output1 is failed" |
| 73 |
- "'parameters are mutually exclusive' in rabbit_basic_fail_output1.msg" |
| 74 |
|
| 75 |
- name: Publish a file that does not exist |
| 76 |
rabbitmq_publish: |
| 77 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 78 |
queue: 'publish_test' |
| 79 |
src: 'aaaaaaajax-loader.gif' |
| 80 |
register: file_missing_fail |
| 81 |
ignore_errors: yes |
| 82 |
delegate_to: localhost |
| 83 |
|
| 84 |
- assert: |
| 85 |
that: |
| 86 |
- "file_missing_fail is failed" |
| 87 |
- "'Unable to open file' in file_missing_fail.msg" |
| 88 |
|
| 89 |
- name: Publish with proto/host/port/user/pass |
| 90 |
rabbitmq_publish: |
| 91 |
proto: amqp |
| 92 |
host: localhost |
| 93 |
port: 5672 |
| 94 |
username: guest |
| 95 |
password: guest |
| 96 |
vhost: '%2F' |
| 97 |
queue: publish_test |
| 98 |
body: Testing with proto/host/port/username/password/vhost |
| 99 |
register: host_port_output |
| 100 |
delegate_to: localhost |
| 101 |
|
| 102 |
- assert: |
| 103 |
that: |
| 104 |
- "host_port_output is not failed" |
| 105 |
|
| 106 |
- name: Publish with host/port/user but missing proto |
| 107 |
rabbitmq_publish: |
| 108 |
host: localhost |
| 109 |
port: 5672 |
| 110 |
username: guest |
| 111 |
password: guest |
| 112 |
vhost: '%2F' |
| 113 |
queue: publish_test |
| 114 |
body: Testing with proto/host/port/username/password/vhost |
| 115 |
ignore_errors: yes |
| 116 |
register: host_port_missing_proto_output |
| 117 |
delegate_to: localhost |
| 118 |
|
| 119 |
- assert: |
| 120 |
that: |
| 121 |
- "host_port_missing_proto_output is failed" |
| 122 |
- "'Connection parameters must be passed via' in host_port_missing_proto_output.msg" |
| 123 |
|
| 124 |
- name: Publish with proto/host/port/user and url |
| 125 |
rabbitmq_publish: |
| 126 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 127 |
proto: amqp |
| 128 |
host: localhost |
| 129 |
port: 5672 |
| 130 |
username: guest |
| 131 |
password: guest |
| 132 |
vhost: '%2F' |
| 133 |
queue: publish_test |
| 134 |
body: Testing with proto/host/port/username/password/vhost |
| 135 |
ignore_errors: yes |
| 136 |
register: host_and_url_output |
| 137 |
delegate_to: localhost |
| 138 |
|
| 139 |
- assert: |
| 140 |
that: |
| 141 |
- "host_and_url_output is failed" |
| 142 |
- "'cannot be specified at the same time' in host_and_url_output.msg" |
| 143 |
|
| 144 |
- name: Publish headers to queue |
| 145 |
rabbitmq_publish: |
| 146 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 147 |
queue: 'publish_test' |
| 148 |
body: blah |
| 149 |
headers: |
| 150 |
myHeader: Value1 |
| 151 |
secondHeader: Value2 |
| 152 |
register: test_headers1 |
| 153 |
ignore_errors: yes |
| 154 |
delegate_to: localhost |
| 155 |
|
| 156 |
- name: Publish headers with file |
| 157 |
rabbitmq_publish: |
| 158 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 159 |
queue: 'publish_test' |
| 160 |
src: "{{ remote_tmp_dir }}/image.gif" |
| 161 |
headers: |
| 162 |
myHeader: Value1 |
| 163 |
secondHeader: Value2 |
| 164 |
register: test_headers2 |
| 165 |
ignore_errors: yes |
| 166 |
delegate_to: localhost |
| 167 |
|
| 168 |
- name: Collect all messages off the publish queue |
| 169 |
set_fact: |
| 170 |
messages: "{{ lookup('community.rabbitmq.rabbitmq', url='amqp://guest:[email protected]:5672/%2F', queue='publish_test') }}" |
| 171 |
delegate_to: localhost |
| 172 |
|
| 173 |
- name: Check contents of published messages |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- messages|length == 5 |
| 177 |
- "'Hello world from ansible module rabbitmq_publish' in messages[0]['msg']" |
| 178 |
- "'text/plain' in messages[0]['content_type']" |
| 179 |
- "'image/gif' in messages[1]['content_type']" |
| 180 |
- "'image.gif' in messages[1]['headers']['filename']" |
| 181 |
- "'Testing with proto/host/port/username/password/vhost' in messages[2]['msg']" |
| 182 |
- messages[4]['headers']['myHeader'] is defined |
| 183 |
- messages[4]['headers']['secondHeader'] is defined |
| 184 |
- "'Value2' in messages[4]['headers']['secondHeader']" |
| 185 |
|
| 186 |
|
| 187 |
- name: Check that queue and exchange are mutually exclusive |
| 188 |
rabbitmq_publish: |
| 189 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 190 |
queue: 'fail_queue' |
| 191 |
exchange: 'fail_exchange' |
| 192 |
body: "Queue and exchange mutually exclusive should fail" |
| 193 |
content_type: "text/plain" |
| 194 |
register: rabbit_mutually_exclusive |
| 195 |
delegate_to: localhost |
| 196 |
ignore_errors: true |
| 197 |
|
| 198 |
- assert: |
| 199 |
that: |
| 200 |
- "rabbit_basic_fail_output1 is failed" |
| 201 |
- "'parameters are mutually exclusive' in rabbit_mutually_exclusive.msg" |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
- name: Add test requisites |
| 210 |
block: |
| 211 |
- name: Add exchange |
| 212 |
rabbitmq_exchange: |
| 213 |
name: "pub_test_exchange" |
| 214 |
type: fanout |
| 215 |
|
| 216 |
- name: Add queue |
| 217 |
rabbitmq_queue: |
| 218 |
name: pub_test_exchange_queue |
| 219 |
|
| 220 |
- name: Add binding |
| 221 |
rabbitmq_binding: |
| 222 |
source: pub_test_exchange |
| 223 |
destination: pub_test_exchange_queue |
| 224 |
type: queue |
| 225 |
register: add_binding |
| 226 |
|
| 227 |
- name: Check that binding succeeds with a change |
| 228 |
assert: |
| 229 |
that: |
| 230 |
- add_binding.changed == true |
| 231 |
|
| 232 |
- name: RabbitMQ basic exchange publish test |
| 233 |
rabbitmq_publish: |
| 234 |
url: "amqp://guest:[email protected]:5672/%2F" |
| 235 |
exchange: 'pub_test_exchange' |
| 236 |
body: "Hello world pub_test_exchange exchange from ansible module rabbitmq_publish" |
| 237 |
content_type: "text/plain" |
| 238 |
register: rabbit_exchange_output1 |
| 239 |
delegate_to: localhost |
| 240 |
|
| 241 |
- assert: |
| 242 |
that: |
| 243 |
- "rabbit_exchange_output1 is not failed" |
| 244 |
- "'pub_test_exchange' in rabbit_exchange_output1.result.msg" |
| 245 |
|
| 246 |
- name: Wait 2 seconds to make sure message has been delivered |
| 247 |
pause: |
| 248 |
seconds: 2 |
| 249 |
|
| 250 |
- name: Retrieve messages from pub_test_exchange_queue |
| 251 |
set_fact: |
| 252 |
rabbitmq_exchange_msg: "{{ lookup('community.rabbitmq.rabbitmq', url='amqp://guest:[email protected]:5672/%2f/pub_test_exchange_queue', queue='pub_test_exchange_queue') }}" |
| 253 |
ignore_errors: yes |
| 254 |
register: rabbitmq_pub_test_exchange_queue |
| 255 |
delegate_to: localhost |
| 256 |
|
| 257 |
- name: Debug out rabbitmq_pub_test_exchange_queue task result |
| 258 |
debug: |
| 259 |
var: rabbitmq_pub_test_exchange_queue |
| 260 |
|
| 261 |
- name: Debug out the set_fact rabbitmq_exchange_msg |
| 262 |
debug: |
| 263 |
var: rabbitmq_exchange_msg |
| 264 |
|
| 265 |
- name: Ensure one message was received |
| 266 |
assert: |
| 267 |
that: |
| 268 |
- "rabbitmq_pub_test_exchange_queue is not failed" |
| 269 |
- rabbitmq_exchange_msg | length == 1 |
| 270 |
|
| 271 |
- name: Ensure first message contains a string |
| 272 |
assert: |
| 273 |
that: |
| 274 |
- "'pub_test_exchange exchange' in rabbitmq_exchange_msg[0].msg" |