Skip to content

Ansible: Dynamic inventory from Proxmox

https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_inventory.html

Needs for ansible:

sudo pacman -S python-requests

ans add Role permissions for API Token.

Simple case

The virtual machines should to have tag:app1, in this example.

# cat proxmox.yml
plugin: community.general.proxmox
url: https://pve1.loc:8006
validate_certs: false
user: root@pam
want_proxmox_nodes_ansible_host: true
token_id: "{{ lookup('ansible.builtin.env', 'ANSIBLE_PVE_TOKEN') }}"
token_secret: "{{ lookup('ansible.builtin.env', 'ANSIBLE_PVE_SECRET') }}"
want_facts: true
filters:
  - proxmox_status == "running"
  - "proxmox_tags_parsed is defined and 'app1' in proxmox_tags_parsed"
groups:
  app1: "'app1' in (proxmox_tags_parsed|list)"
compose:
  ansible_host: proxmox_agent_interfaces[1]["ip-addresses"][0].split('/')[0]

# ansible-inventory -i proxmox.yml app1 --graph
@app1:
  |--app1-node2
  |--app1-node0
  |--app1-master

Checking by ping:

ansible -i proxmox.yml app1 -m ping