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.

Head over to the wiki page for more explanation about this project.


Simply say:

pip install ansinv


Working with ansible inventory hosts

Creating a host object with optional host variables:
host1 = ansinv.AnsibleHost("", affinity=12, scan="no")
Get a host object’s ip/name:
Read and update a host object’s host variables. The hostvars attribute is essentially a dictionary:
host1.hostvars["affinity"] = 5

Working with ansible inventory groups

Creating a group object with optional group variables:
group1 = ansinv.AnsibleGroup("group1", ssh_port=8800)
Read and update a group object’s group variables. The groupvars attribute is essentially a dictionary:
group1.groupvars["ssh_port"] = 22
Adding hosts to a group:
group1.add_hosts(host1, host2, ...) # host1, host2, etc. must already exist
group1.add_hosts(ansinv.AnsibleHost("", hostvar1="value")) # creating and adding hosts at the same time

Please note: Adding a host object actually creates a copy of the host object under the group object. So to make modifications to a host object after it has been added, use method.

Get access to a host object using method:"").hostvars["hostvar1"] = "new value"
Get a list of all host objects in a group:
Establish parent-child relation between groups:
child1 = AnsibleGroup("master")
child2 = AnsibleGroup("worker")
parent = AnsibleGroup("cluster")
parent.add_children(child1, child2)
parent.add_children(parent)   # ValueError when trying to add itself as a child
child1.add_children(parent)   # ValueError when trying to add a parent group as a child
Check whether the group is the parent of a given group:
group1.is_parent_of(group2)   # Returns a bool value
Check whether the group is the child of a given group:
group1.is_child_of(group2)   # Returns a bool value
Get a list of names (not objects) of all child groups:
group1.children   # ["child1", "child2", ...]

For more explanation and a full example please refer the wiki page.

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.3.tar.gz (3.6 kB view hashes)

Uploaded source

Built Distribution

ansinv-2.0.3-py2.py3-none-any.whl (4.2 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