From c494fe5824c1bfcc76cb07079154a19ef088efd3 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 24 Mar 2024 18:25:52 +0100 Subject: [PATCH] [PR #7998/4947786d backport][stable-8] Adds group_by_hostgroups parameter to Icinga2 inventory (#8134) Adds group_by_hostgroups parameter to Icinga2 inventory (#7998) * (lots of commit messages) --------- Co-authored-by: Gianluca Salvo Co-authored-by: Felix Fontein (cherry picked from commit 4947786d3694bee0498c40b54540f67170518b70) Co-authored-by: Gianluca Salvo --- ...nventory-group_by_hostgroups-parameter.yml | 2 ++ plugins/inventory/icinga2.py | 19 ++++++++++++++----- tests/unit/plugins/inventory/test_icinga2.py | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/7998-icinga2-inventory-group_by_hostgroups-parameter.yml diff --git a/changelogs/fragments/7998-icinga2-inventory-group_by_hostgroups-parameter.yml b/changelogs/fragments/7998-icinga2-inventory-group_by_hostgroups-parameter.yml new file mode 100644 index 0000000000..1170a108fd --- /dev/null +++ b/changelogs/fragments/7998-icinga2-inventory-group_by_hostgroups-parameter.yml @@ -0,0 +1,2 @@ +minor_changes: + - icinga2 inventory plugin - adds new parameter ``group_by_hostgroups`` in order to make grouping by Icinga2 hostgroups optional (https://github.com/ansible-collections/community.general/pull/7998). \ No newline at end of file diff --git a/plugins/inventory/icinga2.py b/plugins/inventory/icinga2.py index a418707332..6a6bafdb42 100644 --- a/plugins/inventory/icinga2.py +++ b/plugins/inventory/icinga2.py @@ -63,6 +63,12 @@ DOCUMENTATION = ''' default: address choices: ['name', 'display_name', 'address'] version_added: 4.2.0 + group_by_hostgroups: + description: + - Uses Icinga2 hostgroups as groups. + type: boolean + default: true + version_added: 8.4.0 ''' EXAMPLES = r''' @@ -114,6 +120,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self.ssl_verify = None self.host_filter = None self.inventory_attr = None + self.group_by_hostgroups = None self.cache_key = None self.use_cache = None @@ -248,12 +255,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable): host_attrs['state'] = 'on' else: host_attrs['state'] = 'off' - host_groups = host_attrs.get('groups') self.inventory.add_host(host_name) - for group in host_groups: - if group not in self.inventory.groups.keys(): - self.inventory.add_group(group) - self.inventory.add_child(group, host_name) + if self.group_by_hostgroups: + host_groups = host_attrs.get('groups') + for group in host_groups: + if group not in self.inventory.groups.keys(): + self.inventory.add_group(group) + self.inventory.add_child(group, host_name) # If the address attribute is populated, override ansible_host with the value if host_attrs.get('address') != '': self.inventory.set_variable(host_name, 'ansible_host', host_attrs.get('address')) @@ -283,6 +291,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self.ssl_verify = self.get_option('validate_certs') self.host_filter = self.get_option('host_filter') self.inventory_attr = self.get_option('inventory_attr') + self.group_by_hostgroups = self.get_option('group_by_hostgroups') if self.templar.is_template(self.icinga2_url): self.icinga2_url = self.templar.template(variable=self.icinga2_url, disable_lookups=False) diff --git a/tests/unit/plugins/inventory/test_icinga2.py b/tests/unit/plugins/inventory/test_icinga2.py index e3928b0dbe..859f29d3b0 100644 --- a/tests/unit/plugins/inventory/test_icinga2.py +++ b/tests/unit/plugins/inventory/test_icinga2.py @@ -86,6 +86,8 @@ def get_option(option): return {} elif option == 'strict': return False + elif option == 'group_by_hostgroups': + return True else: return None @@ -96,6 +98,7 @@ def test_populate(inventory, mocker): inventory.icinga2_password = 'password' inventory.icinga2_url = 'https://localhost:5665' + '/v1' inventory.inventory_attr = "address" + inventory.group_by_hostgroups = True # bypass authentication and API fetch calls inventory._check_api = mocker.MagicMock(side_effect=check_api)