Skip to content

Commit

Permalink
[PR #7998/4947786d backport][stable-8] Adds group_by_hostgroups param…
Browse files Browse the repository at this point in the history
…eter to Icinga2 inventory (#8134)

Adds group_by_hostgroups parameter to Icinga2 inventory (#7998)

* (lots of commit messages)

---------

Co-authored-by: Gianluca Salvo <[email protected]>
Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 4947786)

Co-authored-by: Gianluca Salvo <[email protected]>
  • Loading branch information
patchback[bot] and Gianlu authored Mar 24, 2024
1 parent dba4357 commit c494fe5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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).
19 changes: 14 additions & 5 deletions plugins/inventory/icinga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
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'''
Expand Down Expand Up @@ -114,6 +120,7 @@ def __init__(self):
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
Expand Down Expand Up @@ -248,12 +255,13 @@ def _convert_inv(self, json_data):
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'))
Expand Down Expand Up @@ -283,6 +291,7 @@ def parse(self, inventory, loader, path, cache=True):
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)
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/plugins/inventory/test_icinga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def get_option(option):
return {}
elif option == 'strict':
return False
elif option == 'group_by_hostgroups':
return True
else:
return None

Expand All @@ -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)
Expand Down

0 comments on commit c494fe5

Please sign in to comment.