diff --git a/changelogs/fragments/1185-proxmox-ignore-qemu-templates.yml b/changelogs/fragments/1185-proxmox-ignore-qemu-templates.yml new file mode 100644 index 0000000000..34fbdbb0b1 --- /dev/null +++ b/changelogs/fragments/1185-proxmox-ignore-qemu-templates.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - proxmox inventory plugin - ignore QEMU templates altogether instead of skipping the creation of the host in the inventory (https://github.com/ansible-collections/community.general/pull/1185). diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 09e00d34cd..aa9a757a05 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -300,10 +300,12 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): node_qemu_group = self.to_safe('%s%s' % (self.get_option('group_prefix'), ('%s_qemu' % node['node']).lower())) self.inventory.add_group(node_qemu_group) for qemu in self._get_qemu_per_node(node['node']): - if not qemu['template']: - self.inventory.add_host(qemu['name']) - self.inventory.add_child(qemu_group, qemu['name']) - self.inventory.add_child(node_qemu_group, qemu['name']) + if qemu['template']: + continue + + self.inventory.add_host(qemu['name']) + self.inventory.add_child(qemu_group, qemu['name']) + self.inventory.add_child(node_qemu_group, qemu['name']) # get QEMU status self._get_vm_status(node['node'], qemu['vmid'], 'qemu', qemu['name']) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index e03d1b8013..14332e750e 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -75,7 +75,7 @@ def get_json(url): elif url == "https://localhost:8006/api2/json/nodes/testnode/qemu": # _get_qemu_per_node return [{"name": "test-qemu", - "cpus": 1, + "cpus": 1, "mem": 1000, "template": "", "diskread": 0, @@ -89,7 +89,23 @@ def get_json(url): "vmid": "101", "uptime": 1000, "disk": 0, - "status": "running"}] + "status": "running"}, + {"name": "test-qemu-template", + "cpus": 1, + "mem": 0, + "template": 1, + "diskread": 0, + "cpu": 0, + "maxmem": 1000, + "diskwrite": 0, + "netout": 0, + "pid": "1001", + "netin": 0, + "maxdisk": 1000, + "vmid": "9001", + "uptime": 0, + "disk": 0, + "status": "stopped"}] elif url == "https://localhost:8006/api2/json/pools/test": # _get_members_per_pool return {"members": [{"uptime": 1000, @@ -174,6 +190,7 @@ def test_populate(inventory, mocker): # get different hosts host_qemu = inventory.inventory.get_host('test-qemu') + host_qemu_template = inventory.inventory.get_host('test-qemu-template') host_lxc = inventory.inventory.get_host('test-lxc') host_node = inventory.inventory.get_host('testnode') @@ -185,3 +202,6 @@ def test_populate(inventory, mocker): # check if lxc-test has been discovered correctly group_lxc = inventory.inventory.groups['proxmox_all_lxc'] assert group_lxc.hosts == [host_lxc] + + # check if qemu template is not present + assert host_qemu_template is None