Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hostname_field option #1312

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading