From 693a737d16ab2c9edda2de7ab3359a43e312ba6f Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:23:47 +0200 Subject: [PATCH 01/14] Create option to exclude proxmox nodes --- plugins/inventory/proxmox.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index c6f70a45665..ad668241d0e 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -116,6 +116,10 @@ - The default of this option changed from V(true) to V(false) in community.general 6.0.0. type: bool default: false + exclude_nodes: + description: Exclude proxmox nodes (and their groups) from the inventory output. + type: bool + default: false filters: version_added: 4.6.0 description: A list of Jinja templates that allow filtering hosts. @@ -565,7 +569,6 @@ def _populate(self): for group in default_groups: self.inventory.add_group(self._group('all_%s' % (group))) - nodes_group = self._group('nodes') self.inventory.add_group(nodes_group) @@ -607,9 +610,14 @@ def _populate(self): if name is not None: hosts.append(name) + # removes node host at the end of this node-specific loop + if self.exclude_nodes: self.inventory.remove_host(self.inventory.hosts[node['node']]) # gather vm's in pools self._populate_pool_groups(hosts) + # removes the general node-group from the inventory list + if self.exclude_nodes: self.inventory.remove_group(nodes_group) + def parse(self, inventory, loader, path, cache=True): if not HAS_REQUESTS: raise AnsibleError('This module requires Python Requests 1.1.0 or higher: ' @@ -635,8 +643,8 @@ def parse(self, inventory, loader, path, cache=True): if self.get_option('qemu_extended_statuses') and not self.get_option('want_facts'): raise AnsibleError('You must set want_facts to True if you want to use qemu_extended_statuses.') - # read rest of options + self.exclude_nodes = self.get_option('exclude_nodes') self.cache_key = self.get_cache_key(path) self.use_cache = cache and self.get_option('cache') self.host_filters = self.get_option('filters') From bdfbbf138c1c9bb93128f1d9c5cd4a4ccb90796c Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:49:06 +0200 Subject: [PATCH 02/14] improve node exclusion by only remove the top level group --- plugins/inventory/proxmox.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index ad668241d0e..14fbfed6a6c 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -610,8 +610,6 @@ def _populate(self): if name is not None: hosts.append(name) - # removes node host at the end of this node-specific loop - if self.exclude_nodes: self.inventory.remove_host(self.inventory.hosts[node['node']]) # gather vm's in pools self._populate_pool_groups(hosts) From c3454a2c4a62d71142d46473ddeae7b2b702c436 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Sun, 29 Oct 2023 00:09:12 +0200 Subject: [PATCH 03/14] add fragment --- .../fragments/7437-proxmox-inventory-add-exclude-nodes.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml diff --git a/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml b/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml new file mode 100644 index 00000000000..1c3d55e3ba0 --- /dev/null +++ b/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml @@ -0,0 +1,2 @@ +minor_changes: + - proxmox inventory plugin - adds an option to exclude nodes from the dynamic inventory generation and fixes https://github.com/ansible-collections/community.general/issues/6714. The new setting is optional, not using this option will behave as usual. From aa0cacb9fa5a955131fe3ac81a54fbbaee885971 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:09:25 +0100 Subject: [PATCH 04/14] Update changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml Co-authored-by: Felix Fontein --- .../fragments/7437-proxmox-inventory-add-exclude-nodes.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml b/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml index 1c3d55e3ba0..16d9325534c 100644 --- a/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml +++ b/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml @@ -1,2 +1,2 @@ minor_changes: - - proxmox inventory plugin - adds an option to exclude nodes from the dynamic inventory generation and fixes https://github.com/ansible-collections/community.general/issues/6714. The new setting is optional, not using this option will behave as usual. + - proxmox inventory plugin - adds an option to exclude nodes from the dynamic inventory generation. The new setting is optional, not using this option will behave as usual (https://github.com/ansible-collections/community.general/issues/6714, https://github.com/ansible-collections/community.general/pull/7437). From 35ac5a032bca200ebf40bc3b705ccf8467594f58 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:09:48 +0100 Subject: [PATCH 05/14] Update plugins/inventory/proxmox.py Co-authored-by: Felix Fontein --- plugins/inventory/proxmox.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 14fbfed6a6c..0db9ff2f485 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -614,7 +614,8 @@ def _populate(self): self._populate_pool_groups(hosts) # removes the general node-group from the inventory list - if self.exclude_nodes: self.inventory.remove_group(nodes_group) + if self.exclude_nodes: + self.inventory.remove_group(nodes_group) def parse(self, inventory, loader, path, cache=True): if not HAS_REQUESTS: From 02a9e0b56c520403503f70469e77d3cb4d461472 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Wed, 1 Nov 2023 23:36:07 +0100 Subject: [PATCH 06/14] Rework node exclusion --- plugins/inventory/proxmox.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 0db9ff2f485..9cc295d6554 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -570,7 +570,8 @@ def _populate(self): for group in default_groups: self.inventory.add_group(self._group('all_%s' % (group))) nodes_group = self._group('nodes') - self.inventory.add_group(nodes_group) + if not self.exclude_nodes: + self.inventory.add_group(nodes_group) want_proxmox_nodes_ansible_host = self.get_option("want_proxmox_nodes_ansible_host") @@ -580,22 +581,23 @@ def _populate(self): for node in self._get_nodes(): if not node.get('node'): continue - - self.inventory.add_host(node['node']) - if node['type'] == 'node': + if not self.exclude_nodes: + self.inventory.add_host(node['node']) + if node['type'] == 'node' and not self.exclude_nodes: self.inventory.add_child(nodes_group, node['node']) if node['status'] == 'offline': continue # get node IP address - if want_proxmox_nodes_ansible_host: + if want_proxmox_nodes_ansible_host and not self.exclude_nodes: ip = self._get_node_ip(node['node']) self.inventory.set_variable(node['node'], 'ansible_host', ip) # Setting composite variables - variables = self.inventory.get_host(node['node']).get_vars() - self._set_composite_vars(self.get_option('compose'), variables, node['node'], strict=self.strict) + if not self.exclude_nodes: + variables = self.inventory.get_host(node['node']).get_vars() + self._set_composite_vars(self.get_option('compose'), variables, node['node'], strict=self.strict) # add LXC/Qemu groups for the node for ittype in ('lxc', 'qemu'): @@ -613,10 +615,6 @@ def _populate(self): # gather vm's in pools self._populate_pool_groups(hosts) - # removes the general node-group from the inventory list - if self.exclude_nodes: - self.inventory.remove_group(nodes_group) - def parse(self, inventory, loader, path, cache=True): if not HAS_REQUESTS: raise AnsibleError('This module requires Python Requests 1.1.0 or higher: ' From b789b1a3b67fbe1cdff2fafb4d9fc40cee328d16 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Thu, 2 Nov 2023 00:28:34 +0100 Subject: [PATCH 07/14] Update fragement PR number --- ...nodes.yaml => 7461-proxmox-inventory-add-exclude-nodes.yaml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename changelogs/fragments/{7437-proxmox-inventory-add-exclude-nodes.yaml => 7461-proxmox-inventory-add-exclude-nodes.yaml} (82%) diff --git a/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml b/changelogs/fragments/7461-proxmox-inventory-add-exclude-nodes.yaml similarity index 82% rename from changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml rename to changelogs/fragments/7461-proxmox-inventory-add-exclude-nodes.yaml index 16d9325534c..40391342f7e 100644 --- a/changelogs/fragments/7437-proxmox-inventory-add-exclude-nodes.yaml +++ b/changelogs/fragments/7461-proxmox-inventory-add-exclude-nodes.yaml @@ -1,2 +1,2 @@ minor_changes: - - proxmox inventory plugin - adds an option to exclude nodes from the dynamic inventory generation. The new setting is optional, not using this option will behave as usual (https://github.com/ansible-collections/community.general/issues/6714, https://github.com/ansible-collections/community.general/pull/7437). + - proxmox inventory plugin - adds an option to exclude nodes from the dynamic inventory generation. The new setting is optional, not using this option will behave as usual (https://github.com/ansible-collections/community.general/issues/6714, https://github.com/ansible-collections/community.general/pull/7461). From b826962cf931a713628b1d07cb13dedfd891442b Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Thu, 2 Nov 2023 22:23:31 +0100 Subject: [PATCH 08/14] include release version in option Co-authored-by: Felix Fontein --- plugins/inventory/proxmox.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 9cc295d6554..62f4c123fde 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -120,6 +120,7 @@ description: Exclude proxmox nodes (and their groups) from the inventory output. type: bool default: false + version_added: 8.1.0 filters: version_added: 4.6.0 description: A list of Jinja templates that allow filtering hosts. From 323c6760ed558176e8b807340f20acf937e9e49b Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 00:36:36 +0100 Subject: [PATCH 09/14] Clarify description --- plugins/inventory/proxmox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory/proxmox.py b/plugins/inventory/proxmox.py index 62f4c123fde..df593665cf2 100644 --- a/plugins/inventory/proxmox.py +++ b/plugins/inventory/proxmox.py @@ -117,7 +117,7 @@ type: bool default: false exclude_nodes: - description: Exclude proxmox nodes (and their groups) from the inventory output. + description: Exclude proxmox nodes and the nodes-group from the inventory output. type: bool default: false version_added: 8.1.0 From bc1847911a59d85fdcfabf9794231ade8aa4890d Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:34:20 +0100 Subject: [PATCH 10/14] Update unit test --- tests/unit/plugins/inventory/test_proxmox.py | 40 +++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 13832c9387b..848cd38a4d5 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -652,7 +652,8 @@ def test_populate(inventory, mocker): 'facts_prefix': 'proxmox_', 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, - 'qemu_extended_statuses': True + 'qemu_extended_statuses': True, + 'exclude_nodes':False } # bypass authentication and API fetch calls @@ -730,6 +731,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False + 'exclude_nodes':False } # bypass authentication and API fetch calls @@ -743,3 +745,39 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): # make sure that ['prelaunch', 'paused'] are not in the group list for group in ['paused', 'prelaunch']: assert ('%sall_%s' % (inventory.group_prefix, group)) not in inventory.inventory.groups + + +def test_populate_exclude_nodes(inventory, mocker): + # module settings + inventory.proxmox_user = 'root@pam' + inventory.proxmox_password = 'password' + inventory.proxmox_url = 'https://localhost:8006' + inventory.group_prefix = 'proxmox_' + inventory.facts_prefix = 'proxmox_' + inventory.strict = False + + opts = { + 'group_prefix': 'proxmox_', + 'facts_prefix': 'proxmox_', + 'want_facts': True, + 'want_proxmox_nodes_ansible_host': True, + 'qemu_extended_statuses': False, + 'exclude_nodes':True + } + + # bypass authentication and API fetch calls + inventory._get_auth = mocker.MagicMock(side_effect=get_auth) + inventory._get_json = mocker.MagicMock(side_effect=get_json) + inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots) + inventory.get_option = mocker.MagicMock(side_effect=get_option(opts)) + inventory._can_add_host = mocker.MagicMock(return_value=True) + inventory._populate() + + # make sure that nodes are not in the inventory + for node in ['testnode', 'testnode2']: + assert node not in inventory.inventory.hosts + # make sure that nodes group is absent + assert ('%s_nodes' % (inventory.group_prefix) not in inventory.inventory.groups + # make sure that nodes are not in the "ungrouped" group + for node in ['testnode', 'testnode2']: + assert node not in inventory.inventory.get_groups_dict()["ungrouped"] From c061f3e213cccdf832c5302dc3888af05184f61a Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:51:39 +0100 Subject: [PATCH 11/14] Fix typos in unit test --- tests/unit/plugins/inventory/test_proxmox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 848cd38a4d5..4849a728d30 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -730,7 +730,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): 'facts_prefix': 'proxmox_', 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, - 'qemu_extended_statuses': False + 'qemu_extended_statuses': False, 'exclude_nodes':False } @@ -777,7 +777,7 @@ def test_populate_exclude_nodes(inventory, mocker): for node in ['testnode', 'testnode2']: assert node not in inventory.inventory.hosts # make sure that nodes group is absent - assert ('%s_nodes' % (inventory.group_prefix) not in inventory.inventory.groups + assert ('%s_nodes' % (inventory.group_prefix)) not in inventory.inventory.groups # make sure that nodes are not in the "ungrouped" group for node in ['testnode', 'testnode2']: assert node not in inventory.inventory.get_groups_dict()["ungrouped"] From 470b4aa16c23f13881df08eb4af0d33e428a7e8e Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:39:13 +0100 Subject: [PATCH 12/14] Fix additonal typos in test --- tests/unit/plugins/inventory/test_proxmox.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 4849a728d30..c5532643b14 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -653,7 +653,7 @@ def test_populate(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': True, - 'exclude_nodes':False + 'exclude_nodes': False } # bypass authentication and API fetch calls @@ -731,7 +731,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes':False + 'exclude_nodes': False } # bypass authentication and API fetch calls @@ -762,7 +762,7 @@ def test_populate_exclude_nodes(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes':True + 'exclude_nodes': True } # bypass authentication and API fetch calls @@ -778,6 +778,6 @@ def test_populate_exclude_nodes(inventory, mocker): assert node not in inventory.inventory.hosts # make sure that nodes group is absent assert ('%s_nodes' % (inventory.group_prefix)) not in inventory.inventory.groups - # make sure that nodes are not in the "ungrouped" group + # make sure that nodes are not in the "ungrouped" group for node in ['testnode', 'testnode2']: assert node not in inventory.inventory.get_groups_dict()["ungrouped"] From 3e386dd85bca10186fb207eb2dd005b7c9c9463c Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:59:27 +0100 Subject: [PATCH 13/14] Fix CI --- tests/unit/plugins/inventory/test_proxmox.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index c5532643b14..53befe806b8 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -646,6 +646,7 @@ def test_populate(inventory, mocker): inventory.group_prefix = 'proxmox_' inventory.facts_prefix = 'proxmox_' inventory.strict = False + inventory.exclude_nodes = False opts = { 'group_prefix': 'proxmox_', @@ -653,7 +654,7 @@ def test_populate(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': True, - 'exclude_nodes': False + 'exclude_nodes' : False } # bypass authentication and API fetch calls @@ -724,6 +725,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): inventory.group_prefix = 'proxmox_' inventory.facts_prefix = 'proxmox_' inventory.strict = False + inventory.exclude_nodes = False opts = { 'group_prefix': 'proxmox_', @@ -731,7 +733,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes': False + 'exclude_nodes' : False } # bypass authentication and API fetch calls @@ -755,6 +757,7 @@ def test_populate_exclude_nodes(inventory, mocker): inventory.group_prefix = 'proxmox_' inventory.facts_prefix = 'proxmox_' inventory.strict = False + inventory.exclude_nodes = True opts = { 'group_prefix': 'proxmox_', @@ -762,7 +765,7 @@ def test_populate_exclude_nodes(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes': True + 'exclude_nodes' : True } # bypass authentication and API fetch calls From c9e96fe6abd6c145c045c24c9071eead29d8b4e9 Mon Sep 17 00:00:00 2001 From: IamLunchbox <56757745+IamLunchbox@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:13:29 +0100 Subject: [PATCH 14/14] Fixing yet another whitespace pep error --- tests/unit/plugins/inventory/test_proxmox.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/plugins/inventory/test_proxmox.py b/tests/unit/plugins/inventory/test_proxmox.py index 53befe806b8..ea6c84bcdaa 100644 --- a/tests/unit/plugins/inventory/test_proxmox.py +++ b/tests/unit/plugins/inventory/test_proxmox.py @@ -654,7 +654,7 @@ def test_populate(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': True, - 'exclude_nodes' : False + 'exclude_nodes': False } # bypass authentication and API fetch calls @@ -733,7 +733,7 @@ def test_populate_missing_qemu_extended_groups(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes' : False + 'exclude_nodes': False } # bypass authentication and API fetch calls @@ -765,7 +765,7 @@ def test_populate_exclude_nodes(inventory, mocker): 'want_facts': True, 'want_proxmox_nodes_ansible_host': True, 'qemu_extended_statuses': False, - 'exclude_nodes' : True + 'exclude_nodes': True } # bypass authentication and API fetch calls