improve ansible_host in proxmox inventory plugin (#9952)
Some checks failed
EOL CI / EOL Sanity (Ⓐ2.15) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py2.7) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py3.10) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py3.5) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/3/) (push) Has been cancelled
import-galaxy / Test to import built collection artifact with Galaxy importer (push) Has been cancelled
Verify REUSE / check (push) Has been cancelled

* improve ansible_host in proxmox inventory plugin

I had this issue myself and found out there was already an issue thread:

https://github.com/ansible-collections/community.general/issues/5906

this fixes the issue for hope we can all benefit after this gets merged

* f string styling

* add log line for the successful address selection

* remove white space

* add changelog: 9952-proxmox-inventory-plugin-improve-ansible_host.yml

* Update changelogs/fragments/9952-proxmox-inventory-plugin-improve-ansible_host.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Stein van Broekhoven 2025-04-16 20:52:53 +02:00 committed by GitHub
parent 42a161abf5
commit 1243846c3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -366,11 +366,26 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _get_node_ip(self, node):
ret = self._get_json(f"{self.proxmox_url}/api2/json/nodes/{node}/network")
# sort interface by iface name to make selection as stable as possible
ret.sort(key=lambda x: x['iface'])
for iface in ret:
try:
# only process interfaces adhering to these rules
if 'active' not in iface:
self.display.vvv(f"Interface {iface['iface']} on node {node} does not have an active state")
continue
if 'address' not in iface:
self.display.vvv(f"Interface {iface['iface']} on node {node} does not have an address")
continue
if 'gateway' not in iface:
self.display.vvv(f"Interface {iface['iface']} on node {node} does not have a gateway")
continue
self.display.vv(f"Using interface {iface['iface']} on node {node} with address {iface['address']} as node ip for ansible_host")
return iface['address']
except Exception:
return None
continue
return None
def _get_lxc_interfaces(self, properties, node, vmid):
status_key = self._fact('status')