Skip to main content

Generate Ansible Inventory

Project description

|pic1| |pic2| |pic3| |pic4|

A Simple Ansible Inventory Generator
====================================

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

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.

|pic5|

Automation Steps
----------------
Let's say you are using `Terraform <https://www.terraform.io/>`_ tool to bringup cluster on AWS and using `Ansible <https://docs.ansible.com/ansible/latest/index.html>`_ for software provisioning. Following can be the basic steps:

#. Invoke Terraform with desired configuration. After finishing, terraform provides with a `terraform state <https://www.terraform.io/docs/state/index.html>`_ file.
#. Use information (like host IP addresses) from this *terraform.tfstate* file to generate an `inventory <https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html>`_ file to be given to Ansible in the next step.
#. Invoke Ansible `plays <https://docs.ansible.com/ansible/latest/user_guide/playbooks.html>`_ with the inventory file from above step to configure software on the infrastrucure.
#. Cluster is ready to use :)

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

#. 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.
#. 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/generate_inventory.py* 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.

.. |pic1| image:: https://img.shields.io/badge/License-MIT-yellow.svg
:target: https://opensource.org/licenses/MIT

.. |pic2| image:: https://badge.fury.io/py/ansinv.svg
:target: https://pypi.org/project/ansinv

.. |pic3| image:: https://travis-ci.com/rsjethani/ansinv.svg?branch=master
:target: https://travis-ci.com/rsjethani/ansinv

.. |pic4| image:: https://codecov.io/gh/rsjethani/ansinv/branch/master/graph/badge.svg
:target: https://codecov.io/gh/rsjethani/ansinv

.. |pic5| image:: example/docker-swarm-cluster.svg
:scale: 60%


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-1.3.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

ansinv-1.3.3-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ansinv-1.3.3.tar.gz.

File metadata

  • Download URL: ansinv-1.3.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for ansinv-1.3.3.tar.gz
Algorithm Hash digest
SHA256 6d927190e8ed21e1725eadbff42e26997103f03470aa448934683beed6cd5f58
MD5 f6af9aec57624d85ce26f69349ae44ed
BLAKE2b-256 e28bb997af1fb6c61a014dabb801671b801d876469f6436c3192ffa34fcaf665

See more details on using hashes here.

File details

Details for the file ansinv-1.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: ansinv-1.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for ansinv-1.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1fccba228cec8419acc0c2ebd45c12bb4f47996634b0c1df1d44c746cde15b4c
MD5 9e5371747168d92a2ce7284e3c02926a
BLAKE2b-256 84d00f2ba2faf2ac58383327200a2ad7267376bf37fb38226ced1c5a31beeb62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page