From aa2a5d95788ae3ac8ac577a6b514b06031d5f5d5 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 5 Sep 2023 20:50:11 +0200 Subject: [PATCH] [PR #7184/0c03f34f backport][stable-7] plugins/inventory/cobbler: Add exclude/include_mgmt_classes (#7208) plugins/inventory/cobbler: Add exclude/include_mgmt_classes (#7184) (cherry picked from commit 0c03f34f54a4e826483f524571ae7afc3952538f) Co-authored-by: Orion Poplawski --- .../fragments/7184-cobbler-mgmt-classes.yml | 2 ++ plugins/inventory/cobbler.py | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/7184-cobbler-mgmt-classes.yml diff --git a/changelogs/fragments/7184-cobbler-mgmt-classes.yml b/changelogs/fragments/7184-cobbler-mgmt-classes.yml new file mode 100644 index 0000000000..7666eae447 --- /dev/null +++ b/changelogs/fragments/7184-cobbler-mgmt-classes.yml @@ -0,0 +1,2 @@ +minor_changes: + - cobbler inventory plugin - add ``exclude_mgmt_classes`` and ``include_mgmt_classes`` options to exclude or include hosts based on management classes (https://github.com/ansible-collections/community.general/pull/7184). diff --git a/plugins/inventory/cobbler.py b/plugins/inventory/cobbler.py index 6ab8606749..8decbea309 100644 --- a/plugins/inventory/cobbler.py +++ b/plugins/inventory/cobbler.py @@ -42,6 +42,12 @@ DOCUMENTATION = ''' description: Fallback to cached results if connection to cobbler fails. type: boolean default: false + exclude_mgmt_classes: + description: Management classes to exclude from inventory. + type: list + default: [] + elements: str + version_added: 7.4.0 exclude_profiles: description: - Profiles to exclude from inventory. @@ -49,6 +55,12 @@ DOCUMENTATION = ''' type: list default: [] elements: str + include_mgmt_classes: + description: Management classes to include from inventory. + type: list + default: [] + elements: str + version_added: 7.4.0 include_profiles: description: - Profiles to include from inventory. @@ -216,6 +228,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): self.cache_key = self.get_cache_key(path) self.use_cache = cache and self.get_option('cache') + self.exclude_mgmt_classes = self.get_option('exclude_mgmt_classes') + self.include_mgmt_classes = self.get_option('include_mgmt_classes') self.exclude_profiles = self.get_option('exclude_profiles') self.include_profiles = self.get_option('include_profiles') self.group_by = self.get_option('group_by') @@ -265,9 +279,16 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): hostname = host['hostname'] # None interfaces = host['interfaces'] - if self._exclude_profile(host['profile']): - self.display.vvvv('Excluding host %s in profile %s\n' % (host['name'], host['profile'])) - continue + if set(host['mgmt_classes']) & set(self.include_mgmt_classes): + self.display.vvvv('Including host %s in mgmt_classes %s\n' % (host['name'], host['mgmt_classes'])) + else: + if self._exclude_profile(host['profile']): + self.display.vvvv('Excluding host %s in profile %s\n' % (host['name'], host['profile'])) + continue + + if set(host['mgmt_classes']) & set(self.exclude_mgmt_classes): + self.display.vvvv('Excluding host %s in mgmt_classes %s\n' % (host['name'], host['mgmt_classes'])) + continue # hostname is often empty for non-static IP hosts if hostname == '':