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
Metadata
Release files for invgen 1.1.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| invgen-1.1.12.tar.gz | 12.0 kB | Without provenance | Details for invgen-1.1.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| invgen-1.1.12-py3-none-any.whl | Python 3 | none | any | Details for invgen-1.1.12-py3-none-any.whl |
Total release size: 24.0 kB
Release files / invgen-1.1.12.tar.gz
| Download URL | invgen-1.1.12.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22c0d07921d4882a25f345019ae262f925dc6268de88faf16dd9391b0f19d611
|
|
BLAKE2b-256 checksum How to use checksums |
275b59ab4b5d30cbed3595612ee73c9414b892b71fb97f1c52108dbbb7ebe02f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.12
|
Release files / invgen-1.1.12-py3-none-any.whl
| Download URL | invgen-1.1.12-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bebefba7adaed96038ed6273fb73f8a72a83045575c37e83ea51e8450299107d
|
|
BLAKE2b-256 checksum How to use checksums |
84e6473f6f24097ea63d2b4415d5c8b85691d670f1e2b06ce5e0161e2a16d807
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.12
|
Security for invgen 1.1.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.