# Proxmox Dynamic Inventory - pull IP Address for VM & LXC

**URL:** <https://forum.ansible.com/t/proxmox-dynamic-inventory-pull-ip-address-for-vm-lxc/1857>\
**Category:** Get Help\
**Tags:** rhel, playbook, ubuntu\
**Created:** [October 22, 2023, 3:08pm UTC](https://forum.ansible.com/t/proxmox-dynamic-inventory-pull-ip-address-for-vm-lxc/1857 "2023-10-22T15:08:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SixteenOne](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sixteenone/32/459_2.png) [@SixteenOne](https://forum.ansible.com/u/SixteenOne)\
**Post date:** [October 22, 2023, 3:08pm UTC](https://forum.ansible.com/t/proxmox-dynamic-inventory-pull-ip-address-for-vm-lxc/1857/1 "2023-10-22T15:08:19Z")

</div>

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.

```yaml
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

```yaml
... # 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

```yaml
...
"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

```yaml
"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:

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

```

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

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

```

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

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 🙂

---

<div class="post-metadata">

**Author:** ![F546](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@F546](https://forum.ansible.com/u/F546)\
**Post date:** [January 7, 2024, 3:11pm UTC](https://forum.ansible.com/t/proxmox-dynamic-inventory-pull-ip-address-for-vm-lxc/1857/2 "2024-01-07T15:11:10Z")

</div>

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](https://github.com/ansible-collections/community.general/issues/4854)
