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 the glue code between infrastructure bringup/orchestration and software provisioning stages of a one-click deployment.

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

=Installation=

pip install ansinv

=Working with inventory hosts=

Creating a host with optional host variables:

host1 = ansinv.AnsibleHost("192.168.10.11", affinity=12, scan="no")

Get a host’s ip/name using AnsibleHost.name attribute:

print(host1.name)

Read/Update a host’s host variables using AnsibleHost.hostvars attribute:

print(host1.hostvars["scan"])
host1.hostvars["affinity"] = 5
host1.hostvars.update(x=100)

=Working with inventory groups=

Creating a group with optional group variables:

group1 = ansinv.AnsibleGroup("group1", ssh_port=8800)

Get a group’s name using AnsibleGroup.name attribute:

print(group1.name)

Read/Update a group’s group variables using the AnsibleGroup.groupvars attribute:

print(group1.groupvars["ssh_port"])
group1.groupvars["ssh_port"] = 22
group1.groupvars.update(x=100)

Adding hosts to a group using AnsibleGroup.add_hosts method:

group1.add_hosts(host1, host2, ...) # host1, host2, etc. must already exist
group1.add_hosts(ansinv.AnsibleHost("192.168.12.12", hostvar1="value")) # creating and adding hosts at the same time

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

Get access to a member host using AnsibleGroup.host('hostname') method:

group1.host("192.168.1.12").hostvars["hostvar1"] = "new value"

Please note: The host() method will always return the first occurrence of the given ‘hostname’, even if there are multiple hosts with same name in the group. This behavior assumes that even though you are allowed to have multiple hosts with same name but you will never actually require such a case.

Get a list of all host objects in a group using AnsibleGroup.hosts attribute:

print(group1.hosts[0].name)

Establish parent-child relation between groups using AnsibleGroup.add_children method:

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 a parent of given group using AnsibleGroup.is_parent_of method:

group1.is_parent_of(group2)   # Returns a bool value

Check whether the group is a child of given group using AnsibleGroup.is_child_of method:

group1.is_child_of(group2)   # Returns a bool value

Get a list of all child objects using AnsibleGroup.children attribute:

print(group1.children[0].name)

=Working with the inventory itself=

Creating an inventory:

inv = AnsibleInventory()   # empty inventory
inv = AnsibleInventory(AnsibleHost("h1"), AnsibleHost("h2"))   # inventory initialized with two ungrouped hosts

Add (ungrouped) hosts to the inventory using AnsibleInventory.add_hosts method:

h1 = AnsibleHost("h1")
h2 = AnsibleHost("h2")
inv.add_hosts(h1, h2)

Please note: The hosts added directly to the inventory are ‘ungrouped’ hosts i.e. they will not appear under other groups.

Add groups to the inventory using AnsibleInventory.add_groups method:

g1 = AnsibleGroup("g1")
g2 = AnsibleGroup("g2")
inv.add_groups(g1, g2)

Please note: Adding a host/group actually creates a copy of the host/group object under the inventory. So to make modifications to a host/group object after it has been added, use AnsibleInventory.host(hostname)/AnsibleInventory.group(groupname) methods as described below.

Get an ungrouped host object from the inventory using AnsibleInventory.host method:

print(inv.host("h1"))
inv.host("h1").hostvars["somevar"] = 111  # modify an ungrouped host after it has been added to the inventory

Get a group object from the inventory using AnsibleInventory.group('groupname') method:

inv.group("g1").groupvars["x"] = 1111
inv.group("g1").host("h1").hostvars["somevar"] = 333

Please note: The group() method will always return the first occurrence of the given ‘groupname’, even if there are multiple groups with same name in the inventory. This behavior assumes that even though you are allowed to have multiple groups with same name but you will never actually require such a case.

Get a list of all group objects from the inventory using AnsibleInventory.groups attribute:

for grp in inv.groups:
   print(grp.name)

Get the whole inventory as a string object:

The string version of the inventory is in the INI format which you can simply write to a file and pass the file to Ansible.

inv = AnsibleInventory()
...   # add some groups and hosts
print(str(inv))
with open("inventory", "w") as f:
   f.write(str(inv))

For more explanation and a full example please visit 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-3.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansinv-3.0.0-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for ansinv-3.0.0.tar.gz
Algorithm Hash digest
SHA256 9c75cefa15317b3d6859f9385ff210364c795468d1063bc95076faf4eba68c4c
MD5 e4e36d3daed87f20c5b7f4797be5f7fc
BLAKE2b-256 79e424157f95861dd7e0bc1507e371ef0d7c145a63152404ea9519a7c9e1fbbc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ansinv-3.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.9 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.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for ansinv-3.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3cadab7d04dd237b3034a940c7a3becdd3d045fb8723214f52934f134e255d5e
MD5 16b0dcfcd5fb621ebf2406b7d8a9cecf
BLAKE2b-256 83836a1bc9110ba571d154633110cd064eb10fe19f669f20a7eaec2b2a0338bb

See more details on using hashes here.

Supported by

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