diff --git a/changelogs/fragments/1312-add-hostname_field-option.yml b/changelogs/fragments/1312-add-hostname_field-option.yml new file mode 100644 index 00000000..7c9a0a47 --- /dev/null +++ b/changelogs/fragments/1312-add-hostname_field-option.yml @@ -0,0 +1,2 @@ +minor_changes: + - Added option `hostname_field` to ``nb_inventory`` to be able to set the inventory hostname from a field in custom_fields diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index b6ff6c8d..678a4179 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -259,6 +259,11 @@ type: list elements: dict default: [] + hostname_field: + description: + - By default, the inventory hostname is the netbox device name + - If set, sets the inventory hostname from this field in custom_fields instead + default: False """ EXAMPLES = """ @@ -1763,6 +1768,8 @@ 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: + return host["custom_fields"][self.hostname_field] else: return host["name"] or str(uuid.uuid4()) @@ -2140,6 +2147,7 @@ def parse(self, inventory, loader, path, cache=True): self.key = self.get_option("key") self.ca_path = self.get_option("ca_path") self.oob_ip_as_primary_ip = self.get_option("oob_ip_as_primary_ip") + self.hostname_field = self.get_option("hostname_field") self._set_authorization()