Skip to content

Commit 716ca38

Browse files
Inventory: Implement flatten_config_context and flatten_custom_fields options (#194) (#195)
1 parent fa8e54b commit 716ca38

File tree

5 files changed

+587
-3
lines changed

5 files changed

+587
-3
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,25 @@
3636
type: boolean
3737
config_context:
3838
description:
39-
- If True, it adds config-context in host vars.
39+
- If True, it adds config_context in host vars.
4040
- Config-context enables the association of arbitrary data to devices and virtual machines grouped by
4141
region, site, role, platform, and/or tenant. Please check official netbox docs for more info.
4242
default: False
4343
type: boolean
44+
flatten_config_context:
45+
description:
46+
- If config_context is enabled, by default it's added as a host var named config_context.
47+
- If flatten_config_context is set to True, the config context variables will be added directly to the host instead.
48+
default: False
49+
type: boolean
50+
version_added: "0.2.1"
51+
flatten_custom_fields:
52+
description:
53+
- By default, host custom fields are added as a dictionary host var named custom_fields.
54+
- If flatten_custom_fields is set to True, the fields will be added directly to the host instead.
55+
default: False
56+
type: boolean
57+
version_added: "0.2.1"
4458
token:
4559
required: False
4660
description:
@@ -599,7 +613,11 @@ def extract_device_role(self, host):
599613

600614
def extract_config_context(self, host):
601615
try:
602-
return self._pluralize(host["config_context"])
616+
if self.flatten_config_context:
617+
# Don't wrap in an array if we're about to flatten it to separate host vars
618+
return host["config_context"]
619+
else:
620+
return self._pluralize(host["config_context"])
603621
except Exception:
604622
return
605623

@@ -1091,7 +1109,15 @@ def _fill_host_variables(self, host, hostname):
10911109
if attribute == "region":
10921110
attribute = "regions"
10931111

1094-
self.inventory.set_variable(hostname, attribute, extracted_value)
1112+
# Flatten the dict into separate host vars, if enabled
1113+
if isinstance(extracted_value, dict) and (
1114+
(attribute == "config_context" and self.flatten_config_context)
1115+
or (attribute == "custom_fields" and self.flatten_custom_fields)
1116+
):
1117+
for key, value in extracted_value.items():
1118+
self.inventory.set_variable(hostname, key, value)
1119+
else:
1120+
self.inventory.set_variable(hostname, attribute, extracted_value)
10951121

10961122
extracted_primary_ip = self.extract_primary_ip(host=host)
10971123
if extracted_primary_ip:
@@ -1148,6 +1174,8 @@ def parse(self, inventory, loader, path, cache=True):
11481174
self.timeout = self.get_option("timeout")
11491175
self.validate_certs = self.get_option("validate_certs")
11501176
self.config_context = self.get_option("config_context")
1177+
self.flatten_config_context = self.get_option("flatten_config_context")
1178+
self.flatten_custom_fields = self.get_option("flatten_custom_fields")
11511179
self.plurals = self.get_option("plurals")
11521180
self.interfaces = self.get_option("interfaces")
11531181
self.services = self.get_option("services")
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
{
2+
"Test_Cluster": {
3+
"hosts": [
4+
"test100-vm",
5+
"test101-vm",
6+
"test102-vm",
7+
"test103-vm"
8+
]
9+
},
10+
"Test_Cluster_2": {
11+
"hosts": [
12+
"test104-vm"
13+
]
14+
},
15+
"Test_Rack": {
16+
"hosts": [
17+
"R1-Device"
18+
]
19+
},
20+
"_meta": {
21+
"hostvars": {
22+
"R1-Device": {
23+
"device_type": "cisco-test",
24+
"manufacturer": "cisco",
25+
"rack": "Test Rack",
26+
"regions": [],
27+
"role": "core-switch",
28+
"site": "test-site2",
29+
"tags": []
30+
},
31+
"Test Nexus Child One": {
32+
"device_type": "nexus-child",
33+
"manufacturer": "cisco",
34+
"regions": [
35+
"test-region",
36+
"parent-region"
37+
],
38+
"role": "core-switch",
39+
"site": "test-site",
40+
"tags": []
41+
},
42+
"Test Nexus One": {
43+
"device_type": "nexus-parent",
44+
"manufacturer": "cisco",
45+
"regions": [
46+
"test-region",
47+
"parent-region"
48+
],
49+
"role": "core-switch",
50+
"site": "test-site",
51+
"tags": []
52+
},
53+
"TestDeviceR1": {
54+
"device_type": "cisco-test",
55+
"manufacturer": "cisco",
56+
"regions": [
57+
"test-region",
58+
"parent-region"
59+
],
60+
"role": "core-switch",
61+
"site": "test-site",
62+
"tags": []
63+
},
64+
"test100": {
65+
"device_type": "cisco-test",
66+
"manufacturer": "cisco",
67+
"ntp_servers": [
68+
"pool.ntp.org"
69+
],
70+
"regions": [
71+
"test-region",
72+
"parent-region"
73+
],
74+
"role": "core-switch",
75+
"site": "test-site",
76+
"tags": []
77+
},
78+
"test100-vm": {
79+
"cluster": "Test Cluster",
80+
"cluster_group": "test-cluster-group",
81+
"cluster_type": "test-cluster-type",
82+
"regions": [
83+
"test-region",
84+
"parent-region"
85+
],
86+
"site": "test-site",
87+
"tags": []
88+
},
89+
"test101-vm": {
90+
"cluster": "Test Cluster",
91+
"cluster_group": "test-cluster-group",
92+
"cluster_type": "test-cluster-type",
93+
"regions": [
94+
"test-region",
95+
"parent-region"
96+
],
97+
"site": "test-site",
98+
"tags": []
99+
},
100+
"test102-vm": {
101+
"cluster": "Test Cluster",
102+
"cluster_group": "test-cluster-group",
103+
"cluster_type": "test-cluster-type",
104+
"regions": [
105+
"test-region",
106+
"parent-region"
107+
],
108+
"site": "test-site",
109+
"tags": []
110+
},
111+
"test103-vm": {
112+
"cluster": "Test Cluster",
113+
"cluster_group": "test-cluster-group",
114+
"cluster_type": "test-cluster-type",
115+
"regions": [
116+
"test-region",
117+
"parent-region"
118+
],
119+
"site": "test-site",
120+
"tags": []
121+
},
122+
"test104-vm": {
123+
"cluster": "Test Cluster 2",
124+
"cluster_type": "test-cluster-type",
125+
"regions": [],
126+
"tags": []
127+
}
128+
}
129+
},
130+
"all": {
131+
"children": [
132+
"Test_Cluster",
133+
"Test_Cluster_2",
134+
"Test_Rack",
135+
"cisco",
136+
"cisco_test",
137+
"core_switch",
138+
"nexus_child",
139+
"nexus_parent",
140+
"other_region",
141+
"parent_region",
142+
"test_cluster_group",
143+
"test_cluster_type",
144+
"test_site2",
145+
"ungrouped"
146+
]
147+
},
148+
"cisco": {
149+
"hosts": [
150+
"R1-Device",
151+
"Test Nexus Child One",
152+
"Test Nexus One",
153+
"TestDeviceR1",
154+
"test100"
155+
]
156+
},
157+
"cisco_test": {
158+
"hosts": [
159+
"R1-Device",
160+
"TestDeviceR1",
161+
"test100"
162+
]
163+
},
164+
"core_switch": {
165+
"hosts": [
166+
"R1-Device",
167+
"Test Nexus Child One",
168+
"Test Nexus One",
169+
"TestDeviceR1",
170+
"test100"
171+
]
172+
},
173+
"nexus_child": {
174+
"hosts": [
175+
"Test Nexus Child One"
176+
]
177+
},
178+
"nexus_parent": {
179+
"hosts": [
180+
"Test Nexus One"
181+
]
182+
},
183+
"parent_region": {
184+
"children": [
185+
"test_region"
186+
]
187+
},
188+
"test_cluster_group": {
189+
"hosts": [
190+
"test100-vm",
191+
"test101-vm",
192+
"test102-vm",
193+
"test103-vm"
194+
]
195+
},
196+
"test_cluster_type": {
197+
"hosts": [
198+
"test100-vm",
199+
"test101-vm",
200+
"test102-vm",
201+
"test103-vm",
202+
"test104-vm"
203+
]
204+
},
205+
"test_region": {
206+
"children": [
207+
"test_site"
208+
]
209+
},
210+
"test_site": {
211+
"hosts": [
212+
"Test Nexus Child One",
213+
"Test Nexus One",
214+
"TestDeviceR1",
215+
"test100",
216+
"test100-vm",
217+
"test101-vm",
218+
"test102-vm",
219+
"test103-vm"
220+
]
221+
},
222+
"test_site2": {
223+
"hosts": [
224+
"R1-Device"
225+
]
226+
}
227+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
plugin: netbox.netbox.nb_inventory
2+
api_endpoint: "http://localhost:32768"
3+
token: "0123456789abcdef0123456789abcdef01234567"
4+
validate_certs: False
5+
6+
config_context: True
7+
flatten_config_context: True
8+
flatten_custom_fields: True
9+
plurals: False
10+
interfaces: False
11+
services: False
12+
group_names_raw: True
13+
14+
group_by:
15+
- site
16+
- tenant
17+
- rack
18+
- tag
19+
- role
20+
- device_type
21+
- manufacturer
22+
- platform
23+
- region
24+
- cluster
25+
- cluster_group
26+
- cluster_type

0 commit comments

Comments
 (0)