Dynamic Ansible Inventory Generator
Project description
Dynamic Ansible Inventory
This is a dynamic inventory script for Ansible that reads the inventory from a directory. A list of hosts is managed by the user. Each host has metadata that is used to gather variables.
Directory Structure
The structure of the inventory directory is as follows:
hosts/
host1.yaml
host2.yaml
host3.yaml
generated/
host1.yaml
host2.yaml
host3.yaml
metadata/
tags/
tag1.yaml
tag2.yaml
platform/
platform1.yaml
platform2.yaml
my_custom_metadata/
my_custom_metadata1.yaml
my_custom_metadata2.yaml
- The
hostsdirectory contains host files that define the hosts. - The
generateddirectory contains generated host files that are created by the script and used as an inventory. - The
metadatadirectory contains metadata files that can be used to give hosts variables.
Host Configuration
To use the metadata, the host file must have a metadata key that contains a list of metadata files:
metadata:
tags:
- tag1
- tag2
platform: platform1
my_custom_metadata: my_custom_metadata1
Under example/ you can find an example of how to build an inventory.
Variable Templating
Jinja2 can be used inside of variables, because it is parsed by Ansible at runtime:
domain_name: example.com
hostname: ap01
ansible_host: "{{ hostname }}.{{ domain_name }}"
dns_servers:
- 1.1.1.1
- 8.8.8.8
network_interfaces:
- name: eth0
type: ethernet
state: up
ip: 192.168.1.100
netmask: 255.255.255.0
gateway: 192.168.1.1
dns: "{{ dns_servers }}"
Installation
Install with pip install -U invgen or uv tool install -U invgen.
Usage
Generate Inventory
# set the environment variable INVGEN_SOURCE to the path of the inventory directory
export INVGEN_SOURCE="$PWD/example/"
# generate the host files
invgen generate --verbose
# clean and regenerate host files
invgen generate --verbose --clean
# regenerate on file change
invgen generate --verbose --watch
Create New Hosts and Metadata
# Create a new host from a template
invgen new host -t example/templates/host.yaml -d example/hosts --name "ap02.test.local" -o "platform=raspberry-pi-4 provider=self-hosted services=pihole,dnsmasq"
# Create a new metadata file
invgen new metadata --metadata-type platform --name "raspberry-pi-5" -s example/
Validate Inventory
# Validate all host files
invgen validate hosts
Use with Ansible
# set the environment variable INVGEN_SOURCE to the path of the inventory directory
export INVGEN_SOURCE="$PWD/example/"
# run playbook with the inventory
ansible-playbook -i $(which invgen-ansible) playbook.yaml
# explore the inventory
❯ ansible-inventory -i $(which invgen-ansible) --graph
@all:
|--@ungrouped:
|--@environment_production:
| |--ap01.test.local
|--@provider_self-hosted:
| |--ap01.test.local
|--@hardware_raspberry-pi-4:
| |--ap01.test.local
|--@os_rhel-9:
| |--ap01.test.local
|--@services_podman-rootless:
| |--ap01.test.local
|--@tags_selinux-deactivated:
| |--ap01.test.local
Variable Merging
When multiple metadata sources define the same variable:
- For dictionaries: Keys are merged with host-specific values taking precedence
- For lists: Items are combined with duplicates removed
- For scalar values: The last processed value (host-specific) takes precedence
Advanced Features
Templating
You can use Jinja2 templates to generate host files:
---
metadata:
platform: {{ platform }}
provider: {{ provider }}
services: {{ services }}
ansible_host: {{ name }}
Watching for Changes
The --watch flag allows the tool to automatically regenerate the inventory when files change:
invgen generate --verbose --watch
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file invgen-1.1.12.tar.gz.
File metadata
- Download URL: invgen-1.1.12.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22c0d07921d4882a25f345019ae262f925dc6268de88faf16dd9391b0f19d611
|
|
| MD5 |
f1eb6904d16c6394d590eb989ce8075f
|
|
| BLAKE2b-256 |
275b59ab4b5d30cbed3595612ee73c9414b892b71fb97f1c52108dbbb7ebe02f
|
File details
Details for the file invgen-1.1.12-py3-none-any.whl.
File metadata
- Download URL: invgen-1.1.12-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bebefba7adaed96038ed6273fb73f8a72a83045575c37e83ea51e8450299107d
|
|
| MD5 |
3ec3b0b22525afe36120b45f1e838d68
|
|
| BLAKE2b-256 |
84e6473f6f24097ea63d2b4415d5c8b85691d670f1e2b06ce5e0161e2a16d807
|