Proxmox Dynamic Inventory - pull IP Address for VM & LXC

I am new to Ansible and I am using Proxmox to create my Linux (LXC) Containers and VM’s

As I am creating and destroying them as I test new things out, I am trying to use Dynamic Inventory against Proxmox to set the username and IP Addresses so SSH can connect without manually typing anything out

I followed the Proxmox Guide and have set out my Groups based on Tags that I have set in Proxmox.

plugin: community.general.proxmox
url: "{{ lookup('ansible.builtin.ini', 'url', section='proxmox', file='var_pmx.ini') }}"
user: "{{ lookup('ansible.builtin.ini', 'pmx_user', section='proxmox', file='var_pmx.ini') }}"
password: "{{ lookup('ansible.builtin.ini', 'pmx_password', section='proxmox', file='var_pmx.ini') }}"
want_facts: true
groups:
  docker: "'docker' in (proxmox_tags_parsed|list)"
  ansible_group: "'ansible' in (proxmox_tags_parsed|list)"
  ubuntu: "'ubuntu' in (proxmox_tags_parsed|list)"
  redhat: "'rhel' in (proxmox_tags_parsed|list)"
  container: "'amd64' in (proxmox_arch)" 

compose:
  ansible_host: proxmox_agent_interfaces | selectattr('name', 'in', 'eth0,ens18') | map(attribute='ip-addresses') | flatten | first | ipaddr('address')

This is working well for the grouping and returns the IP’s for VM’s but not LXC’s

... # Ubuntu LXC
"kafka": {
  "ansible_ssh_user": "root",
  "proxmox_arch": "amd64",
...
... # Ubuntu VM
"minke": {
  "ansible_host": "192.168.1.190",
  "ansible_ssh_user": "ubuntu",
...

Outputting the details with ansible-inventory --list --vars > pmx.txt to a text file, I can see the differences in Networks between:

LXC

...
"proxmox_net0": {
    "bridge": "vmbr0",
    "firewall": "1",
    "gw": "192.168.1.254",
    "hwaddr": "66:2D:5E:1C:7E:96",
    "ip": "192.168.1.8/24",
    "name": "eth0",
    "type": "veth"
}, 
...

VM

"proxmox_agent_interfaces": [
    {
        "ip-addresses": [
            "127.0.0.1/8",
            "::1/128"
        ],
        "mac-address": "00:00:00:00:00:00",
        "name": "lo"
    },
    {
        "ip-addresses": [
            "192.168.1.190/24",
            "fe80::9c5f:1dff:fecc:5fc6/64"
        ],
        "mac-address": "9e:5f:1d:cc:5f:c6",
        "name": "ens18"
    },

I can output the LXC IP by changing the compose in my Proxmox file:

...
compose:
  ansible_host: proxmox_ipconfig0.ip | default(proxmox_net0.ip) | ipaddr('address')
...

This now works for LXC’s but not for VM’s

...
"kafka": {
    "ansible_host": "192.168.1.8",
    "ansible_ssh_user": "root",
...

I have tried to use a if statement, but failed miserably :frowning:

In my head I need to do an if statement based on either the Group or proxmox_vmtype which will be either lxc or qemu

I have tried to put as much detail as I could, I couldn’t find anything online to answer this, so hopefully someone has a useful suggestion here :slight_smile:

I got a weird issue. To me it doens’t work at all if my vm/lxc are not resolved via /etc/hosts on my ansible controller or dns.

i think this solve the issue : Dynamic inventory for Proxmox can not compose ansible_host variables for qemu vms · Issue #4854 · ansible-collections/community.general · GitHub