Skip to content

Commit dc89f96

Browse files
Enhancement: Add local_context_data to inventory plugin & netbox_device (#258)
1 parent 3f98d20 commit dc89f96

File tree

11 files changed

+219
-49
lines changed

11 files changed

+219
-49
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
default: False
5050
type: boolean
5151
version_added: "0.2.1"
52+
flatten_local_context_data:
53+
description:
54+
- If I(local_context_data) is enabled, by default it's added as a host var named local_context_data.
55+
- If flatten_local_context_data is set to True, the config context variables will be added directly to the host instead.
56+
default: False
57+
type: boolean
58+
version_added: "0.2.4"
5259
flatten_custom_fields:
5360
description:
5461
- By default, host custom fields are added as a dictionary host var named custom_fields.
@@ -341,6 +348,7 @@ def group_extractors(self):
341348
"memory": self.extract_memory,
342349
"vcpus": self.extract_vcpus,
343350
"config_context": self.extract_config_context,
351+
"local_context_data": self.extract_local_context_data,
344352
"custom_fields": self.extract_custom_fields,
345353
"region": self.extract_regions,
346354
"cluster": self.extract_cluster,
@@ -467,6 +475,16 @@ def extract_config_context(self, host):
467475
except Exception:
468476
return
469477

478+
def extract_local_context_data(self, host):
479+
try:
480+
if self.flatten_local_context_data:
481+
# Don't wrap in an array if we're about to flatten it to separate host vars
482+
return host["local_context_data"]
483+
else:
484+
return self._pluralize(host["local_context_data"])
485+
except Exception:
486+
return
487+
470488
def extract_manufacturer(self, host):
471489
try:
472490
return self._pluralize(
@@ -1153,6 +1171,10 @@ def _fill_host_variables(self, host, hostname):
11531171
if isinstance(extracted_value, dict) and (
11541172
(attribute == "config_context" and self.flatten_config_context)
11551173
or (attribute == "custom_fields" and self.flatten_custom_fields)
1174+
or (
1175+
attribute == "local_context_data"
1176+
and self.flatten_local_context_data
1177+
)
11561178
):
11571179
for key, value in extracted_value.items():
11581180
self.inventory.set_variable(hostname, key, value)
@@ -1247,6 +1269,7 @@ def parse(self, inventory, loader, path, cache=True):
12471269
self.validate_certs = self.get_option("validate_certs")
12481270
self.config_context = self.get_option("config_context")
12491271
self.flatten_config_context = self.get_option("flatten_config_context")
1272+
self.flatten_local_context_data = self.get_option("flatten_local_context_data")
12501273
self.flatten_custom_fields = self.get_option("flatten_custom_fields")
12511274
self.plurals = self.get_option("plurals")
12521275
self.interfaces = self.get_option("interfaces")

plugins/module_utils/netbox_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,9 @@ def _normalize_data(self, data):
796796
:params data (dict): Original data from Netbox module
797797
"""
798798
for k, v in data.items():
799-
if isinstance(v, dict):
799+
if k == "local_context_data":
800+
pass
801+
elif isinstance(v, dict):
800802
for subk, subv in v.items():
801803
sub_data_type = QUERY_TYPES.get(subk, "q")
802804
if sub_data_type == "slug":

plugins/modules/netbox_device.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
- must exist in Netbox
155155
required: false
156156
type: dict
157+
local_context_data:
158+
description:
159+
- Arbitrary JSON data to define the devices configuration variables.
160+
required: false
161+
type: dict
157162
required: true
158163
type: dict
159164
state:
@@ -222,6 +227,8 @@
222227
device_type: C9410R
223228
device_role: Core Switch
224229
site: Main
230+
local_context_data:
231+
bgp: "65000"
225232
tags:
226233
- Schnozzberry
227234
state: present
@@ -296,6 +303,7 @@ def main():
296303
vc_priority=dict(required=False, type="int"),
297304
comments=dict(required=False, type="str"),
298305
tags=dict(required=False, type="list"),
306+
local_context_data=dict(required=False, type="dict"),
299307
custom_fields=dict(required=False, type="dict"),
300308
),
301309
),

tests/integration/targets/inventory/files/test-inventory-legacy.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"cisco-test"
1111
],
1212
"is_virtual": false,
13+
"local_context_data": [
14+
null
15+
],
1316
"manufacturers": [
1417
"cisco"
1518
],
@@ -32,6 +35,9 @@
3235
"nexus-parent"
3336
],
3437
"is_virtual": false,
38+
"local_context_data": [
39+
null
40+
],
3541
"manufacturers": [
3642
"cisco"
3743
],
@@ -71,6 +77,9 @@
7177
"cluster_type": "test-cluster-type",
7278
"custom_fields": {},
7379
"is_virtual": true,
80+
"local_context_data": [
81+
null
82+
],
7483
"regions": [],
7584
"services": [
7685
{
@@ -104,6 +113,9 @@
104113
"cisco-test"
105114
],
106115
"is_virtual": false,
116+
"local_context_data": [
117+
null
118+
],
107119
"manufacturers": [
108120
"cisco"
109121
],
@@ -126,6 +138,13 @@
126138
"cisco-test"
127139
],
128140
"is_virtual": false,
141+
"local_context_data": [
142+
{
143+
"ntp_servers": [
144+
"pool.ntp.org"
145+
]
146+
}
147+
],
129148
"manufacturers": [
130149
"cisco"
131150
],
@@ -197,6 +216,9 @@
197216
"cluster_type": "test-cluster-type",
198217
"custom_fields": {},
199218
"is_virtual": true,
219+
"local_context_data": [
220+
null
221+
],
200222
"regions": [
201223
"test-region",
202224
"parent-region"
@@ -213,6 +235,9 @@
213235
"cluster_type": "test-cluster-type",
214236
"custom_fields": {},
215237
"is_virtual": true,
238+
"local_context_data": [
239+
null
240+
],
216241
"regions": [
217242
"test-region",
218243
"parent-region"
@@ -229,6 +254,9 @@
229254
"cluster_type": "test-cluster-type",
230255
"custom_fields": {},
231256
"is_virtual": true,
257+
"local_context_data": [
258+
null
259+
],
232260
"regions": [
233261
"test-region",
234262
"parent-region"
@@ -245,6 +273,9 @@
245273
"cluster_type": "test-cluster-type",
246274
"custom_fields": {},
247275
"is_virtual": true,
276+
"local_context_data": [
277+
null
278+
],
248279
"regions": [
249280
"test-region",
250281
"parent-region"
@@ -260,6 +291,9 @@
260291
"cluster_type": "test-cluster-type",
261292
"custom_fields": {},
262293
"is_virtual": true,
294+
"local_context_data": [
295+
null
296+
],
263297
"regions": [],
264298
"services": [],
265299
"tags": []

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ validate_certs: False
66
config_context: True
77
flatten_config_context: True
88
flatten_custom_fields: True
9+
flatten_local_context_data: True
910
plurals: False
1011
interfaces: True
1112
services: True

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
"device_type": "cisco-test",
7575
"display": "test100",
7676
"is_virtual": false,
77+
"local_context_data": {
78+
"ntp_servers": [
79+
"pool.ntp.org"
80+
]
81+
},
7782
"manufacturer": "cisco",
7883
"regions": [
7984
"test-region",

tests/integration/targets/inventory/files/test-inventory-plurals-flatten.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"cisco-test"
2929
],
3030
"is_virtual": false,
31+
"local_context_data": [
32+
null
33+
],
3134
"manufacturers": [
3235
"cisco"
3336
],
@@ -48,6 +51,9 @@
4851
"nexus-parent"
4952
],
5053
"is_virtual": false,
54+
"local_context_data": [
55+
null
56+
],
5157
"manufacturers": [
5258
"cisco"
5359
],
@@ -64,6 +70,9 @@
6470
"cluster": "Test Cluster 2",
6571
"cluster_type": "test-cluster-type",
6672
"is_virtual": true,
73+
"local_context_data": [
74+
null
75+
],
6776
"regions": [],
6877
"tags": []
6978
},
@@ -75,6 +84,9 @@
7584
"cisco-test"
7685
],
7786
"is_virtual": false,
87+
"local_context_data": [
88+
null
89+
],
7890
"manufacturers": [
7991
"cisco"
8092
],
@@ -95,6 +107,13 @@
95107
"cisco-test"
96108
],
97109
"is_virtual": false,
110+
"local_context_data": [
111+
{
112+
"ntp_servers": [
113+
"pool.ntp.org"
114+
]
115+
}
116+
],
98117
"manufacturers": [
99118
"cisco"
100119
],
@@ -115,6 +134,9 @@
115134
"cluster_group": "test-cluster-group",
116135
"cluster_type": "test-cluster-type",
117136
"is_virtual": true,
137+
"local_context_data": [
138+
null
139+
],
118140
"regions": [
119141
"test-region",
120142
"parent-region"
@@ -129,6 +151,9 @@
129151
"cluster_group": "test-cluster-group",
130152
"cluster_type": "test-cluster-type",
131153
"is_virtual": true,
154+
"local_context_data": [
155+
null
156+
],
132157
"regions": [
133158
"test-region",
134159
"parent-region"
@@ -143,6 +168,9 @@
143168
"cluster_group": "test-cluster-group",
144169
"cluster_type": "test-cluster-type",
145170
"is_virtual": true,
171+
"local_context_data": [
172+
null
173+
],
146174
"regions": [
147175
"test-region",
148176
"parent-region"
@@ -157,6 +185,9 @@
157185
"cluster_group": "test-cluster-group",
158186
"cluster_type": "test-cluster-type",
159187
"is_virtual": true,
188+
"local_context_data": [
189+
null
190+
],
160191
"regions": [
161192
"test-region",
162193
"parent-region"
@@ -170,6 +201,9 @@
170201
"cluster": "Test Cluster 2",
171202
"cluster_type": "test-cluster-type",
172203
"is_virtual": true,
204+
"local_context_data": [
205+
null
206+
],
173207
"regions": [],
174208
"tags": []
175209
}

0 commit comments

Comments
 (0)