From d43c5ab65aa8e2f4274aa13cbbb04a9df6b2cbfb Mon Sep 17 00:00:00 2001 From: Antoine Dunn Date: Thu, 31 Oct 2024 17:16:54 +0100 Subject: [PATCH] Add ability to template device name --- changelogs/fragments/1346-template-any-field-host.yml | 2 ++ plugins/inventory/nb_inventory.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/1346-template-any-field-host.yml diff --git a/changelogs/fragments/1346-template-any-field-host.yml b/changelogs/fragments/1346-template-any-field-host.yml new file mode 100644 index 000000000..fb8aff9fc --- /dev/null +++ b/changelogs/fragments/1346-template-any-field-host.yml @@ -0,0 +1,2 @@ +minor_changes: + - Add the ability to use any field to generate the host name in inventory diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index b06d0fa7f..4b4b18ce2 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -1775,8 +1775,12 @@ def extract_name(self, host): # Use virtual chassis name if set by the user. if self.virtual_chassis_name and self._get_host_virtual_chassis_master(host): return host["virtual_chassis"]["name"] or str(uuid.uuid4()) - elif self.hostname_field: + elif self.hostname_field and self.hostname_field in host["custom_fields"]: return host["custom_fields"][self.hostname_field] + elif self.hostname_field and ( + self.hostname_field in host or "." in self.hostname_field + ): + return self._compose(self.hostname_field, host) or str(uuid.uuid4()) else: return host["name"] or str(uuid.uuid4())