Ansible – Running from Message Queue

Ansible – Running from Message Queue

Programmatically running Ansible

It’s really easy to run Ansible programmatically. I use Python to access the Ansible APIs and run playbooks. It’s also possible to use a templating engine like Jinja2 to build playbooks but that’s for another blog post later.

Check out servers for hackers on how to hook into Ansible’s API. Very simple and straight forward, I built a reusable component for myself based on this.

Message Queue

So far I’ve used ActiveMQ
ansible activemq

and Amazon SQS.
ansible amazonsqs

SQS has its own libraries you can use that are different than my code example provided below. It’s way easier to use SQS and I believe there’s a free tier which can be utilized. There are some benefits to running your own, like no additional costs and you have complete control and oversight on the infrastructure/data.

Code Example

Check out my project on Github to see an example on how I consume a message queue to run playbooks. This is just a skeleton project, I’ve built more robust systems on the same project. You can review the JSON object returned from running a playbook to validate completion or if there are errors. I like to get into the habit of utilizing the queue for retries. When you get a failure, put the message in the back of the queue with a retry flag.

What to do

  • Utilize JSON in the Queue. I was using a few different languages and in some I could produce message of certain object types and this would make it awkward to work with in python
  • Catch errors in the JSON returned from a playbook run. I liked to include an “attempt” or “retry” variable in the JSON or map message. The consumer would utilize these variables to decide what to do and put messages back in the queue to re-attempt on a failure.
  • Build an object to run playbooks simply using the Ansible APIs

Leave a Reply

Your email address will not be published.