Skip to content

Commit

Permalink
add hostname_field option (#1312)
Browse files Browse the repository at this point in the history
* add hostname_field option

* add hostname_field changelog fragment

* add newline to hostname_field changelog fragment

---------

Co-authored-by: Richard Kearsley <[email protected]>
  • Loading branch information
rkearsley and Richard Kearsley authored Sep 12, 2024
1 parent 090ae9b commit 030601b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1312-add-hostname_field-option.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 030601b

Please sign in to comment.