Running Ansible 2 Programmatically

Ansible 2 is out, and that means it’s time to upgrade the previous article on Running Ansible Programmatically for Ansible 2, which has significant API changes under the hood.

Use Case

At work, we are spinning up hosted trials for a historically on-premise product (no multi-tenancy).

To ensure things run smoothly, we need logging and reporting of Ansible runs while these trials spin up or are updated.

Each server instance (installation of the application) has unique data (license, domain configuration, etc).

Running Ansible programmatically gives us the most flexibility and has proven to be a reliable way to go about this.

At the cost of some code complexity, we gain the ability to avoid generating host and variable files on the system (although dynamic host generations may have let us do this – this is certainly not THE WAY™ to solve this problem).

There are ways to accomplish all of this without diving into Ansible’s internal API. The trade-off seems to be control vs “running a CLI command from another program”, which always feels dirty.

Learning some of Ansible’s internals was fun, so I went ahead and did it.

Overall, there’s just more control when calling the Ansible API programmatically.