community.general icon indicating copy to clipboard operation
community.general copied to clipboard

Proxmox inventory plugin not pulling ansible_host IP for Proxmox node

Open dot-mike opened this issue 2 years ago • 5 comments

Summary

I need to be able to pull the IP-address of the Proxmox-node dynamically in my inventory so I've enabled the option want_proxmox_nodes_ansible_host. And when looking at the docs and the code I don't fully agree with the behavior. (Also there is a typo in the docs ansbile_host)

Whether to set ansbile_host for proxmox nodes. When set to true (default), will use the first available interface. This can be different from what you expect.

It says the first available interface, but the current implementation of the code returns "None" even if the first interface is not in use. The code should check if the first available interface is in use and possibly if it has a gateway.

The code I'm referring to https://github.com/ansible-collections/community.general/blob/main/plugins/inventory/proxmox.py#L346-L353

Given the following returned JSON response for network API the current implementation will return "None" due to interface vmbr0 not having an address-key. The correct interface in my opinion is vmbr2.201 because it is next on the list AND has an IP-address in the address-key

"data": [
        {
            "active": 1,
            "bridge_fd": "0",
            "bridge_ports": "",
            "bridge_stp": "off",
            "families": [
                "inet"
            ],
            "iface": "vmbr0",
            "method": "manual",
            "method6": "manual",
            "priority": 8,
            "type": "bridge"
        },
        {
            "active": 1,
            "address": "172.20.1.2",
            "autostart": 1,
            "cidr": "172.20.1.2/24",
            "comments": "PROXMOX MGMT VLAN\n",
            "exists": null,
            "families": [
                "inet"
            ],
            "gateway": "172.20.1.1",
            "iface": "vmbr2.201",
            "method": "static",
            "method6": "manual",
            "netmask": "24",
            "options": [
                "up ip route add 172.20.0.0/16 dev vmbr2.201 via 172.20.1.1",
                "up ip route add 10.100.10.0/24 dev vmbr2.201 via 172.20.1.1"
            ],
            "priority": 7,
            "type": "vlan",
            "vlan-id": "201",
            "vlan-raw-device": "vmbr2"
        },
        {
            "active": 1,
            "exists": 1,
            "families": [
                "inet"
            ],
            "iface": "enp7s0f1",
            "method": "manual",
            "method6": "manual",
            "priority": 4,
            "type": "eth"
        },
        {
            "active": 1,
            "exists": 1,
            "families": [
                "inet"
            ],
            "iface": "eno0",
            "method": "manual",
            "method6": "manual",
            "priority": 3,
            "type": "eth"
        },
        {
            "active": 1,
            "autostart": 1,
            "bridge_fd": "0",
            "bridge_ports": "",
            "bridge_stp": "off",
            "bridge_vids": "2-4094",
            "bridge_vlan_aware": 1,
            "comments": "INT LAN\n",
            "families": [
                "inet"
            ],
            "iface": "vmbr2",
            "method": "manual",
            "method6": "manual",
            "priority": 6,
            "type": "bridge"
        },
        {
            "active": 1,
            "autostart": 1,
            "bridge_fd": "0",
            "bridge_ports": "enp7s0f1",
            "bridge_stp": "off",
            "bridge_vids": "2-4094",
            "bridge_vlan_aware": 1,
            "comments": "WAN\n",
            "families": [
                "inet"
            ],
            "iface": "vmbr1",
            "method": "manual",
            "method6": "manual",
            "priority": 5,
            "type": "bridge"
        }
    ]

Issue Type

Bug Report

Component Name

proxmox.py

Ansible Version

$ ansible --version
ansible [core 2.13.7]
  config file = /home/mike/code/proxmox-setup/ansible/ansible.cfg
  configured module search path = ['/home/mike/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/mike/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0]
  jinja version = 3.0.3
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general
# /usr/lib/python3/dist-packages/ansible_collections
Collection        Version
----------------- -------
community.general 5.8.3  

# /home/mike/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 6.2.0

Configuration

$ ansible-config dump --only-changed
ANSIBLE_NOCOWS(/home/mike/code/proxmox-setup/ansible/ansible.cfg) = True
DEFAULT_EXECUTABLE(/home/mike/code/proxmox-setup/ansible/ansible.cfg) = /bin/bash
DEFAULT_HOST_LIST(/home/mike/code/proxmox-setup/ansible/ansible.cfg) = ['/home/mike/code/proxmox-setup/ansible/hosts']
DEFAULT_REMOTE_USER(/home/mike/code/proxmox-setup/ansible/ansible.cfg) = ansible
HOST_KEY_CHECKING(/home/mike/code/proxmox-setup/ansible/ansible.cfg) = False

OS / Environment

No response

Steps to Reproduce

inventory:

# inventory.homelab.proxmox.yml
plugin: community.general.proxmox
url: https://dns-of-proxmox-host:8006/
user: ansible@pve
password: ansiblepass
validate_certs: no
want_facts: yes

# need this to pull `ansible_host` for the proxmox node
want_proxmox_nodes_ansible_host: true
compose:
  ansible_host: proxmox_net0.ip | default(proxmox_ipconfig0.ip) | ipaddr('address')

Example command showing ansible_host not being set

$ansible -i inventories/inventory.homelab.proxmox.yml all -l 'pve' -m ansible.builtin.debug -a "msg={{hostvars[inventory_hostname]['ansible_host']}}"

pve | SUCCESS => {
    "msg": ""
}

Expected Results

ansible_host should be set to the first available IP-address on an interface.

Example command showing it working:

$ansible -i inventories/inventory.homelab.proxmox.yml all -l 'pve' -m ansible.builtin.debug -a "msg={{hostvars[inventory_hostname]['ansible_host']}}"

pve | SUCCESS => {
    "msg": "192.168.10.1"
}

Actual Results

No response

Code of Conduct

  • [X] I agree to follow the Ansible Code of Conduct

dot-mike avatar Jan 28 '23 10:01 dot-mike