From 2a40169da582277f005440b850e7561e3a82543a Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 29 May 2023 21:08:59 +0200 Subject: [PATCH] [PR #6502/8586adcd backport][stable-7] plugins/inventory/cobbler: Add option to use system name for inventory (#6593) plugins/inventory/cobbler: Add option to use system name for inventory (#6502) * plugins/inventory/cobbler: Add option to use system name for inventory hostname (#6492) * plugins/inventory/cobbler: Add warning for systems with empty profiles (cherry picked from commit 8586adcd518803ec7d874f65c4d6b75092e8345c) Co-authored-by: Orion Poplawski --- .../6502-cobbler-inventory_hostname.yml | 3 +++ plugins/inventory/cobbler.py | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/6502-cobbler-inventory_hostname.yml diff --git a/changelogs/fragments/6502-cobbler-inventory_hostname.yml b/changelogs/fragments/6502-cobbler-inventory_hostname.yml new file mode 100644 index 0000000000..5a2a76f2bf --- /dev/null +++ b/changelogs/fragments/6502-cobbler-inventory_hostname.yml @@ -0,0 +1,3 @@ +minor_changes: + - cobbler inventory plugin - add ``inventory_hostname`` option to allow using the system name for the inventory hostname (https://github.com/ansible-collections/community.general/pull/6502). + - cobbler inventory plugin - add warning for systems with empty profiles (https://github.com/ansible-collections/community.general/pull/6502). diff --git a/plugins/inventory/cobbler.py b/plugins/inventory/cobbler.py index 936a409aeb..1b8382e331 100644 --- a/plugins/inventory/cobbler.py +++ b/plugins/inventory/cobbler.py @@ -56,6 +56,15 @@ DOCUMENTATION = ''' default: [] elements: str version_added: 4.4.0 + inventory_hostname: + description: + - What to use for the ansible inventory hostname. + - By default the networking hostname is used if defined, otherwise the DNS name of the management or first non-static interface. + - If set to I(system), the cobbler system name is used. + type: str + choices: [ 'hostname', 'system' ] + default: hostname + version_added: 7.1.0 group_by: description: Keys to group hosts by type: list @@ -201,6 +210,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): self.exclude_profiles = self.get_option('exclude_profiles') self.include_profiles = self.get_option('include_profiles') self.group_by = self.get_option('group_by') + self.inventory_hostname = self.get_option('inventory_hostname') for profile in self._get_profiles(): if profile['parent']: @@ -238,7 +248,10 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): for host in self._get_systems(): # Get the FQDN for the host and add it to the right groups - hostname = host['hostname'] # None + if self.inventory_hostname == 'system': + hostname = host['name'] # None + else: + hostname = host['hostname'] # None interfaces = host['interfaces'] if self._exclude_profile(host['profile']): @@ -262,8 +275,11 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): self.display.vvvv('Added host %s hostname %s\n' % (host['name'], hostname)) # Add host to profile group - group_name = self._add_safe_group_name(host['profile'], child=hostname) - self.display.vvvv('Added host %s to profile group %s\n' % (hostname, group_name)) + if host['profile'] != '': + group_name = self._add_safe_group_name(host['profile'], child=hostname) + self.display.vvvv('Added host %s to profile group %s\n' % (hostname, group_name)) + else: + self.display.warning('Host %s has an empty profile\n' % (hostname)) # Add host to groups specified by group_by fields for group_by in self.group_by: