[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 <gianluca.salvo@gruppomol.it>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 4947786d36)

Co-authored-by: Gianluca Salvo <Gianlu@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2024-03-24 18:25:52 +01:00 committed by GitHub
commit c494fe5824
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 5 deletions

View file

@ -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)