From 98c13919c570a63564240186a4cac5094231e035 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 25 Jun 2025 16:50:17 +0200 Subject: [PATCH 1/4] Added support for hardcoded strings in hostgroups --- modules/hostgroups.py | 31 ++++++++++++++++++------------- modules/tools.py | 1 + 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/hostgroups.py b/modules/hostgroups.py index 58bb057..6da099a 100644 --- a/modules/hostgroups.py +++ b/modules/hostgroups.py @@ -117,19 +117,24 @@ class Hostgroup: for hg_item in hg_items: # Check if requested data is available as option for this host if hg_item not in self.format_options: - # Check if a custom field exists with this name - cf_data = self.custom_field_lookup(hg_item) - # CF does not exist - if not cf_data["result"]: - msg = ( - f"Unable to generate hostgroup for host {self.name}. " - f"Item type {hg_item} not supported." - ) - self.logger.error(msg) - raise HostgroupError(msg) - # CF data is populated - if cf_data["cf"]: - hg_output.append(cf_data["cf"]) + if hg_item.startswith(("'", '"')) and hg_item.endswith(("'", '"')): + hg_item = hg_item.strip("\'") + hg_item = hg_item.strip('\"') + hg_output.append(hg_item) + else: + # Check if a custom field exists with this name + cf_data = self.custom_field_lookup(hg_item) + # CF does not exist + if not cf_data["result"]: + msg = ( + f"Unable to generate hostgroup for host {self.name}. " + f"Item type {hg_item} not supported." + ) + self.logger.error(msg) + raise HostgroupError(msg) + # CF data is populated + if cf_data["cf"]: + hg_output.append(cf_data["cf"]) continue # Check if there is a value associated to the variable. # For instance, if a device has no location, do not use it with hostgroup calculation diff --git a/modules/tools.py b/modules/tools.py index ae7a12b..dacab20 100644 --- a/modules/tools.py +++ b/modules/tools.py @@ -160,6 +160,7 @@ def verify_hg_format( if ( hg_object not in allowed_objects[hg_type] and hg_object not in allowed_objects["cfs"][hg_type] + and not hg_object.startswith(('"',"'")) ): e = ( f"Hostgroup item {hg_object} is not valid. Make sure you" From 3910e0de2d04a779d79a0aeee9c355657319b4d4 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 25 Jun 2025 16:54:12 +0200 Subject: [PATCH 2/4] Updated docs --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index ef280cc..169aca0 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,17 @@ in `config.py` the script will render a full region path of all parent regions for the hostgroup name. `traverse_site_groups` controls the same behaviour for site_groups. +**Hardcoded text** + +You can add hardcoded text in the hostgroup format by using quotes, this will +insert the literal text: + +```python +hostgroup_format = "'MyDevices'/location/role" +``` + +In this case, the prefix MyDevices will be used for all generated groups. + **Custom fields** You can use the value of custom fields for hostgroup generation. This allows From e82c098e26603d07fc67aff9ed6ccd9c9e101c8c Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 25 Jun 2025 17:00:04 +0200 Subject: [PATCH 3/4] corrected linting error --- modules/hostgroups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hostgroups.py b/modules/hostgroups.py index 6da099a..954c218 100644 --- a/modules/hostgroups.py +++ b/modules/hostgroups.py @@ -120,7 +120,7 @@ class Hostgroup: if hg_item.startswith(("'", '"')) and hg_item.endswith(("'", '"')): hg_item = hg_item.strip("\'") hg_item = hg_item.strip('\"') - hg_output.append(hg_item) + hg_output.append(hg_item) else: # Check if a custom field exists with this name cf_data = self.custom_field_lookup(hg_item) From 161b310ba379afdd91be7240549823205db85e41 Mon Sep 17 00:00:00 2001 From: Raymond Kuiper Date: Wed, 25 Jun 2025 17:07:46 +0200 Subject: [PATCH 4/4] corrected linting error --- modules/hostgroups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hostgroups.py b/modules/hostgroups.py index 954c218..5916c0a 100644 --- a/modules/hostgroups.py +++ b/modules/hostgroups.py @@ -120,7 +120,7 @@ class Hostgroup: if hg_item.startswith(("'", '"')) and hg_item.endswith(("'", '"')): hg_item = hg_item.strip("\'") hg_item = hg_item.strip('\"') - hg_output.append(hg_item) + hg_output.append(hg_item) else: # Check if a custom field exists with this name cf_data = self.custom_field_lookup(hg_item)