Skip to content

Commit 4c4218a

Browse files
authored
Inventory: Add option to use VC name instead of device name (#383)
1 parent 5deb79f commit 4c4218a

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@
165165
type: int
166166
default: 4000
167167
version_added: "0.2.1"
168+
virtual_chassis_name:
169+
description:
170+
- When a device is part of a virtual chassis, use the virtual chassis name as the Ansible inventory hostname.
171+
- The host var values will be from the virtual chassis master.
172+
type: boolean
173+
default: False
168174
compose:
169175
description: List of custom ansible host vars to create from the device object fetched from NetBox
170176
default: {}
@@ -1191,7 +1197,11 @@ def extract_name(self, host):
11911197
# An host in an Ansible inventory requires an hostname.
11921198
# name is an unique but not required attribute for a device in NetBox
11931199
# We default to an UUID for hostname in case the name is not set in NetBox
1194-
return host["name"] or str(uuid.uuid4())
1200+
# Use virtual chassis name if set by the user.
1201+
if self.virtual_chassis_name and self._get_host_virtual_chassis_master(host):
1202+
return host["virtual_chassis"]["name"] or str(uuid.uuid4())
1203+
else:
1204+
return host["name"] or str(uuid.uuid4())
11951205

11961206
def generate_group_name(self, grouping, group):
11971207

@@ -1447,5 +1457,6 @@ def parse(self, inventory, loader, path, cache=True):
14471457
self.query_filters = self.get_option("query_filters")
14481458
self.device_query_filters = self.get_option("device_query_filters")
14491459
self.vm_query_filters = self.get_option("vm_query_filters")
1460+
self.virtual_chassis_name = self.get_option("virtual_chassis_name")
14501461

14511462
self.main()

tests/integration/targets/inventory-latest/files/test-inventory-options.json

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,6 @@
4444
},
4545
"tags": []
4646
},
47-
"Test Nexus One": {
48-
"custom_fields": {},
49-
"device_type": "nexus-parent",
50-
"display": "Test Nexus One",
51-
"is_virtual": false,
52-
"manufacturer": "cisco",
53-
"regions": [
54-
"test-region",
55-
"parent-region"
56-
],
57-
"role": "core-switch",
58-
"site": "test-site",
59-
"status": {
60-
"label": "Active",
61-
"value": "active"
62-
},
63-
"tags": []
64-
},
6547
"Test VM With Spaces": {
6648
"cluster": "Test Cluster 2",
6749
"cluster_type": "test-cluster-type",
@@ -98,6 +80,24 @@
9880
},
9981
"tags": []
10082
},
83+
"VC1": {
84+
"custom_fields": {},
85+
"device_type": "nexus-parent",
86+
"display": "Test Nexus One",
87+
"is_virtual": false,
88+
"manufacturer": "cisco",
89+
"regions": [
90+
"test-region",
91+
"parent-region"
92+
],
93+
"role": "core-switch",
94+
"site": "test-site",
95+
"status": {
96+
"label": "Active",
97+
"value": "active"
98+
},
99+
"tags": []
100+
},
101101
"test100": {
102102
"custom_fields": {},
103103
"device_type": "cisco-test",
@@ -229,8 +229,8 @@
229229
"cisco": {
230230
"hosts": [
231231
"R1-Device",
232-
"Test Nexus One",
233232
"TestDeviceR1",
233+
"VC1",
234234
"test100"
235235
]
236236
},
@@ -244,8 +244,8 @@
244244
"core_switch": {
245245
"hosts": [
246246
"R1-Device",
247-
"Test Nexus One",
248247
"TestDeviceR1",
248+
"VC1",
249249
"test100"
250250
]
251251
},
@@ -261,14 +261,13 @@
261261
},
262262
"jinja_test_group": {
263263
"hosts": [
264-
"Test Nexus One",
265264
"Test VM With Spaces",
266265
"TestDeviceR1"
267266
]
268267
},
269268
"nexus_parent": {
270269
"hosts": [
271-
"Test Nexus One"
270+
"VC1"
272271
]
273272
},
274273
"parent_rack_group": {
@@ -316,8 +315,8 @@
316315
},
317316
"test_site": {
318317
"hosts": [
319-
"Test Nexus One",
320318
"TestDeviceR1",
319+
"VC1",
321320
"test100",
322321
"test100-vm",
323322
"test101-vm",

tests/integration/targets/inventory-latest/files/test-inventory-options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ plurals: False
1515
interfaces: False
1616
services: False
1717
group_names_raw: True
18+
virtual_chassis_name: True
1819

1920
group_by:
2021
- site

0 commit comments

Comments
 (0)