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



and Amazon SQS.



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