Skip to main content

Generate Ansible Inventory

Project description

pic1 pic2 pic3 pic4

A Simple Ansible Inventory Generator


This simple library makes it easier to write glue code between infrastructure bringup/deployment and software provisioning stages of a one-click deployment.


Simply say:

pip install ansinv

Make sure you are using correct pip (pip/pip3) depending on your Python environment.

Use Case

Picture the following Docker Swarm cluster that needs to be brought up and provisioned all in one go. The infrastructure has one manager, two workers, and a cluster heath monitoring node.


Automation Steps

Let’s say you are using Terraform tool to bringup cluster on AWS and using Ansible for software provisioning. Following can be the basic steps:

  1. Invoke Terraform with desired configuration. After finishing, terraform provides with a terraform state file.

  2. Use information (like host IP addresses) from this terraform.tfstate file to generate an inventory file to be given to Ansible in the next step.

  3. Invoke Ansible plays with the inventory file from above step to configure software on the infrastrucure.

  4. Cluster is ready to use :)

Generating the Inventory file

How are we going to generate inventory file in step #2 above? Options:

  1. Creating manually by looking at the terraform state file and hand-editing the inventory file. It works! but not inline with one-click deployment philosophy.

  2. Writing code to parse the terraform state file for required information and generating inventory file on the fly. This is where ansinv can help.

While writing code to parse terraform state file and generating the the inventory file in INI format, we have to write quite a bit of boiler-plate code which doesn’t change from one deployment to another. For example: generating inventory file’s INI template, dealing with hosts, groups and their inter-relation, adding hostvars and groupvars at proper place in the inventory file etc. Here ansinv comes to the rescue and wraps all that common functionality into a nice and easy to use API.

A Code Example

If you browse the example directory, you will find example/ python file. This file reads example/terraform.tfstate.example and generates inventory file similar to example/inventory.example. FYI, the ordering of sections in the inventory file can be different every time you generate it. But this doesn’t matter.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansinv-2.0.1.tar.gz (3.6 kB view hashes)

Uploaded source

Built Distribution

ansinv-2.0.1-py2.py3-none-any.whl (4.3 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page