Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #7998/4947786d backport][stable-8] Adds group_by_hostgroups parameter to Icinga2 inventory #8134

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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