Skip to content

Commit 030601b

Browse files
rkearsleyRichard Kearsley
andauthored
add hostname_field option (#1312)
* add hostname_field option * add hostname_field changelog fragment * add newline to hostname_field changelog fragment --------- Co-authored-by: Richard Kearsley <richard@uniquex.com>
1 parent 090ae9b commit 030601b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Added option `hostname_field` to ``nb_inventory`` to be able to set the inventory hostname from a field in custom_fields

plugins/inventory/nb_inventory.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@
259259
type: list
260260
elements: dict
261261
default: []
262+
hostname_field:
263+
description:
264+
- By default, the inventory hostname is the netbox device name
265+
- If set, sets the inventory hostname from this field in custom_fields instead
266+
default: False
262267
"""
263268

264269
EXAMPLES = """
@@ -1763,6 +1768,8 @@ def extract_name(self, host):
17631768
# Use virtual chassis name if set by the user.
17641769
if self.virtual_chassis_name and self._get_host_virtual_chassis_master(host):
17651770
return host["virtual_chassis"]["name"] or str(uuid.uuid4())
1771+
elif self.hostname_field:
1772+
return host["custom_fields"][self.hostname_field]
17661773
else:
17671774
return host["name"] or str(uuid.uuid4())
17681775

@@ -2140,6 +2147,7 @@ def parse(self, inventory, loader, path, cache=True):
21402147
self.key = self.get_option("key")
21412148
self.ca_path = self.get_option("ca_path")
21422149
self.oob_ip_as_primary_ip = self.get_option("oob_ip_as_primary_ip")
2150+
self.hostname_field = self.get_option("hostname_field")
21432151

21442152
self._set_authorization()
21452153

0 commit comments

Comments
 (0)