Ansible has had support for managing the setup of RabbitMQ servers for some time now. However, being new to managing RabbitMQ with Ansible, I was surprised to find that there were not any modules to publish to, or, consume messages from RabbitMQ.
It seemed that an Ansible lookup plugin would be ideal for a RabbitMQ basic blocking consumer, which, would allow consuming messages off a queue and iterating over the resulting messages using with_items or loop.
In Ansible 2.8 you will be able to do this with the lookup rabbitmq plugin. (Before running this you will require the python pika module, and, Ansible 2.8).
- name: Lookup queue set_fact: contents: "{{ lookup('rabbitmq', url='amqp://guest:[email protected]:5672/%2F', queue='simple_test') }}" - name: Picked up message from the queue debug: msg: "the queue contained {{ contents }}"
Output from running:
TASK [Picked up message from the queue] ******************************************************************************************************************************************************************** ok: [localhost] => { "msg": "the queue contained [{'exchange': u'', 'delivery_mode': 1, 'routing_key': u'simple_test', 'message_count': 0, 'content_type': u'text/plain', 'msg': u'Test message to queue simple_test', 'redelivered': False, 'delivery_tag': 1}]" }
Note: In a latest update, the headers will be returned in the output as well.
Publishing to a queue
You’ll probably also want to publish some messages onto a queue from Ansible. Maybe you’ll want to process something with Ansible and publish the results to a queue?
In 2.8 you will also be able to do this with the rabbitmq_publish module:
- name: Publish some text to the hello queue rabbitmq_publish: url: "amqp://guest:[email protected]:5672/%2F" queue: 'hello' body: "Hello world from ansible module rabitmq_publish" content_type: "text/plain" register: output delegate_to: localhost - debug: var: output # Testing random queue - name: Publish to a random queue rabbitmq_publish: url: "amqp://guest:[email protected]:5672/%2F" body: "RANDOM QUEUE POST" content_type: "text/plain" register: output_random delegate_to: localhost - debug: var: output_random - name: Publish an image to a queue rabbitmq_publish: url: "amqp://guest:[email protected]:5672/%2F" queue: 'hello' src: 'ajax-loader.gif' register: output2 delegate_to: localhost - debug: var: output2
Output from publish play run
TASK [Task 1] ********************************************************************************************************************************************************************************************** changed: [localhost -> localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "output": { "changed": true, "failed": false, "result": { "content_type": "text/plain", "msg": "Successfully published to queue hello", "queue": "hello" } } } TASK [Post to random queue] ******************************************************************************************************************************************************************************** changed: [localhost -> localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "output_random": { "changed": true, "failed": false, "result": { "content_type": "text/plain", "msg": "Successfully published to queue amq.gen-Hkzn8BdgS98PZtSaM3Mdhg", "queue": "amq.gen-Hkzn8BdgS98PZtSaM3Mdhg" } } } TASK [rabbitmq_publish] ************************************************************************************************************************************************************************************ changed: [localhost -> localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "output2": { "changed": true, "failed": false, "result": { "content_type": "image/gif", "msg": "Successfully published to queue hello", "queue": "hello" } } }
Want to see more examples of how the lookup plugin and publish module works, check out the integration tests.