Dynamic inventory on proxmox
To create a dynamix inventory on proxmox, we need an inventory plugin.
The collection community.proxmox has the proxmox inventory plugin and this is what we are going to use.
No programming, no special code, just the plugin in an execution environment that we will also need to deploy machines and containers on proxmox.
This inventory is nothing more than a gitlab repository with some files in there, using a certain structure, as we will explain.
excution environment
As we said before, we will need a execution environment to run the inventory plugin. This environment can be built with the following procedure:
Building excution environments
content of the execution environment
If you use the build instructions on this site, the ee_vars.yml, should contain the following vars:
ee_image_name: ee_proxmox
ee_python:
- dnspython
- proxmoxer
- requests
- netaddr
ee_collections:
- community.general
- community.proxmox
- ansible.utils
ee_system:
- openssh-clients [platform:redhat]
use_ansible_cfg: true
basic_image: quay.io/rockylinux/rockylinux:9.5-minimal
ee_build_steps:
ee_version: 1.0
This will build the EE you can use for this inventory.
gitlab project
The gitlab project for the inventory should contain the following files/directories:
.
├── group_vars
│ ├── proxmox.yml
│ ├── lxc.yml
│ ├── qemu.yml
│ ├── ansible.yml
│ ├── proxmox_all_lxc.yml
│ └── proxmox_all_qemu.yml
└── inventory
├── 00-static-groups.yml
└── 01-inventory.proxmox.yml
The functional part of this niventory is in the invnetory directory, there are 2 files here:
1) The first is 00-static-groups.yml.
In this file we layout the structure of the inventory with the groups the plugin genereates, this must be read first, otherwise this will not work correctly.
You can use your tags for the hosts on proxmox here to order your groups.
all:
children:
proxmox:
children:
ansible:
children:
lxc:
qemu:
proxmox_all_lxc:
proxmox_all_qemu:
proxmox_cluster:
children:
proxmox_nodes:
This will structure the inventory using the base groups I defined and the groups the plugin reads from the proxmox cluster.
ansible, lxc and qemu are tags I use on virtual machines to order the variables they need for ansible.
2) The second file is 01-inventory.proxmox.yml
In this file the inventory plugin is configured to retrieve the inventory information from the cluster.
The plugin configuration is as follows:
---
plugin: community.proxmox.proxmox
url: https://proxmox01.homelab:8006
validate_certs: false
want_facts: true
# Instead of login with password, proxmox supports api token authentication since release 6.2.
user: <audit user on cluster>
password: <password>
# Group vms by tags for reference in playbooks.
keyed_groups:
- key: proxmox_tags_parsed
separator: ""
prefix: ""
compose:
ansible_host: proxmox_name
Ensure that the password is used as a credential, for simplicity, I left it in here.
There are a lot of other possibilities to group your hosts read from proxmox, for that, read the plugin documentation.
The files in the group_vars directory map to the groups in the inventory and contain static variables for various playbooks that use the inventory.
the inventory in config as code
Below the inventory as defined in configuration as code, using the execution environment ee-proxmox, we built for this.
controller_inventories:
- name: MGT_inventory_proxmox
description: MGT proxmox inventory
organization: MGT
controller_inventory_sources:
- name: MGT_inventory_proxmox
description:
organization: MGT
source: scm
source_project: MGT_proxmox_inventory
execution_environment: ee-proxmox
inventory: MGT_inventory_proxmox
update_on_launch: true
overwrite_vars: true
overwrite: true
Do not specify source_path: / in the inventory_source definition, it will break your inventory!