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.

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

Installation

Simply say:

pip install ansinv

Usage

Working with ansible inventory hosts

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

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:
print(group1.groupvars["ssh_port"])
group1.groupvars["ssh_port"] = 22
group1.groupvars.update(x=100)
Adding hosts to a group:
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 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 AnsibleGroup.host(hostname) method.

Get access to a host object using AnsibleGroup.host(hostname) method:
group1.host("192.168.1.12").hostvars["hostvar1"] = "new value"
Get a list of all host objects in a group:
group1.hosts
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 Python 2 Python 3

Supported by

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