Skip to main content

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.

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 hosts directory contains host files that define the hosts.

The generated directory contains generated host files that are created by the script and used as an inventory.

The metadata directory contains metadata files that can be used to give hosts variables. To use the metadata, the host file must have a metadata key that contains a list of metadata files.

metadata:
  tags:
    - tag1
  platform: platform1
  my_custom_metadata: my_custom_metadata1

Under example/ you can find an example of how to use the dynamic inventory script.

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 }}"

Usage

Install with pip install -U invgen or uv tool install -U invgen.

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

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
 [ERROR]: 2024-11-23 19:22:29,537 - INFO - inventory - Generating inventory from /home/fwrage/dev/invgen/example
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
@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

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

invgen-0.4.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

invgen-0.4.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file invgen-0.4.0.tar.gz.

File metadata

  • Download URL: invgen-0.4.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for invgen-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ea750232e0ae9f43b33f09560f51edec9c5872899375be70c3f7cb4d65ed1854
MD5 b2f3b459429a3637041e8a511320bb28
BLAKE2b-256 cd6ed02de1c1eb2eecad2481762d3250dacbd9a90f8ed5685d39f09da881fc45

See more details on using hashes here.

File details

Details for the file invgen-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: invgen-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for invgen-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85b8071edc945b488e52a7706e540c0d2590872169e78214b69b394282569473
MD5 a2be41cfdf2848b100df686bc2afd9cf
BLAKE2b-256 e6267c57236a6d752149d6c17efe729cfd79b78e9a1835fb01eb9803b22089a0

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