From 69bb4420d8605fa7521db6b9cb7c3e441207bbd2 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 16 Apr 2025 21:06:02 +0200 Subject: [PATCH] [PR #9952/1243846c backport][stable-9] improve ansible_host in proxmox inventory plugin (#10002) improve ansible_host in proxmox inventory plugin (#9952) * 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 --------- Co-authored-by: Felix Fontein (cherry picked from commit 1243846c3a0d24d82bbf6970e4dbcb1ac245efa5) Co-authored-by: Stein van Broekhoven --- ...ox-inventory-plugin-improve-ansible_host.yml | 2 ++ plugins/inventory/proxmox.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/9952-proxmox-inventory-plugin-improve-ansible_host.yml diff --git a/changelogs/fragments/9952-proxmox-inventory-plugin-improve-ansible_host.yml b/changelogs/fragments/9952-proxmox-inventory-plugin-improve-ansible_host.yml new file mode 100644 index 0000000000..dc973835c7 --- /dev/null +++ b/changelogs/fragments/9952-proxmox-inventory-plugin-improve-ansible_host.yml @@ -0,0 +1,2 @@ +bugfixes: + - proxmox inventory plugin - fix ``ansible_host`` staying empty for certain Proxmox nodes (https://github.com/ansible-collections/community.general/issues/5906, https://github.com/ansible-collections/community.general/pull/9952). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index f20c82e81f..975c344aef 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -362,11 +362,26 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def _get_node_ip(self, node): ret = self._get_json("%s/api2/json/nodes/%s/network" % (self.proxmox_url, node)) + # 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')