1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[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
parent dba4357b65
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

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

View file

@ -63,6 +63,12 @@ DOCUMENTATION = '''
default: address default: address
choices: ['name', 'display_name', 'address'] choices: ['name', 'display_name', 'address']
version_added: 4.2.0 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''' EXAMPLES = r'''
@ -114,6 +120,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
self.ssl_verify = None self.ssl_verify = None
self.host_filter = None self.host_filter = None
self.inventory_attr = None self.inventory_attr = None
self.group_by_hostgroups = None
self.cache_key = None self.cache_key = None
self.use_cache = None self.use_cache = None
@ -248,12 +255,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
host_attrs['state'] = 'on' host_attrs['state'] = 'on'
else: else:
host_attrs['state'] = 'off' host_attrs['state'] = 'off'
host_groups = host_attrs.get('groups')
self.inventory.add_host(host_name) self.inventory.add_host(host_name)
for group in host_groups: if self.group_by_hostgroups:
if group not in self.inventory.groups.keys(): host_groups = host_attrs.get('groups')
self.inventory.add_group(group) for group in host_groups:
self.inventory.add_child(group, host_name) 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 the address attribute is populated, override ansible_host with the value
if host_attrs.get('address') != '': if host_attrs.get('address') != '':
self.inventory.set_variable(host_name, 'ansible_host', 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.ssl_verify = self.get_option('validate_certs')
self.host_filter = self.get_option('host_filter') self.host_filter = self.get_option('host_filter')
self.inventory_attr = self.get_option('inventory_attr') 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): if self.templar.is_template(self.icinga2_url):
self.icinga2_url = self.templar.template(variable=self.icinga2_url, disable_lookups=False) self.icinga2_url = self.templar.template(variable=self.icinga2_url, disable_lookups=False)

View file

@ -86,6 +86,8 @@ def get_option(option):
return {} return {}
elif option == 'strict': elif option == 'strict':
return False return False
elif option == 'group_by_hostgroups':
return True
else: else:
return None return None
@ -96,6 +98,7 @@ def test_populate(inventory, mocker):
inventory.icinga2_password = 'password' inventory.icinga2_password = 'password'
inventory.icinga2_url = 'https://localhost:5665' + '/v1' inventory.icinga2_url = 'https://localhost:5665' + '/v1'
inventory.inventory_attr = "address" inventory.inventory_attr = "address"
inventory.group_by_hostgroups = True
# bypass authentication and API fetch calls # bypass authentication and API fetch calls
inventory._check_api = mocker.MagicMock(side_effect=check_api) inventory._check_api = mocker.MagicMock(side_effect=check_api)