48
48
# in order of precedence
49
49
- name: NETBOX_TOKEN
50
50
- name: NETBOX_API_KEY
51
+ plurals:
52
+ description:
53
+ - If True, all host vars are contained inside single-element arrays for legacy compatibility. Group names will be plural (ie. "sites_mysite" instead of "site_mysite")
54
+ default: True
55
+ type: boolean
56
+ version_added: "0.1.11"
51
57
interfaces:
52
58
description:
53
59
- If True, it adds the device or virtual machine interface information in host vars.
61
67
type: boolean
62
68
version_added: "2.0"
63
69
group_by:
64
- description: Keys used to create groups.
70
+ description: Keys used to create groups. The 'plurals' option controls which of these are valid.
65
71
type: list
66
72
choices:
67
73
- sites
74
+ - site
68
75
- tenants
76
+ - tenant
69
77
- racks
78
+ - rack
70
79
- tags
80
+ - tag
71
81
- device_roles
82
+ - device_role
72
83
- device_types
84
+ - device_type
73
85
- manufacturers
86
+ - manufacturer
74
87
- platforms
88
+ - platform
75
89
default: []
76
90
group_names_raw:
77
91
description: Will not add the group_by choice name to the group names
@@ -285,23 +299,50 @@ def get_resource_list(self, api_url):
285
299
286
300
@property
287
301
def group_extractors (self ):
288
- return {
289
- "sites" : self .extract_site ,
290
- "tenants" : self .extract_tenant ,
291
- "racks" : self .extract_rack ,
292
- "tags" : self .extract_tags ,
293
- "disk" : self .extract_disk ,
294
- "memory" : self .extract_memory ,
295
- "vcpus" : self .extract_vcpus ,
296
- "device_roles" : self .extract_device_role ,
297
- "platforms" : self .extract_platform ,
298
- "device_types" : self .extract_device_type ,
299
- "services" : self .extract_services ,
300
- "config_context" : self .extract_config_context ,
301
- "manufacturers" : self .extract_manufacturer ,
302
- "interfaces" : self .extract_interfaces ,
303
- "custom_fields" : self .extract_custom_fields ,
304
- }
302
+
303
+ if self .plurals :
304
+ return {
305
+ "sites" : self .extract_site ,
306
+ "tenants" : self .extract_tenant ,
307
+ "racks" : self .extract_rack ,
308
+ "tags" : self .extract_tags ,
309
+ "disk" : self .extract_disk ,
310
+ "memory" : self .extract_memory ,
311
+ "vcpus" : self .extract_vcpus ,
312
+ "device_roles" : self .extract_device_role ,
313
+ "platforms" : self .extract_platform ,
314
+ "device_types" : self .extract_device_type ,
315
+ "services" : self .extract_services ,
316
+ "config_context" : self .extract_config_context ,
317
+ "manufacturers" : self .extract_manufacturer ,
318
+ "interfaces" : self .extract_interfaces ,
319
+ "custom_fields" : self .extract_custom_fields ,
320
+ }
321
+ else :
322
+ return {
323
+ "site" : self .extract_site ,
324
+ "tenant" : self .extract_tenant ,
325
+ "rack" : self .extract_rack ,
326
+ "tag" : self .extract_tags ,
327
+ "disk" : self .extract_disk ,
328
+ "memory" : self .extract_memory ,
329
+ "vcpus" : self .extract_vcpus ,
330
+ "device_role" : self .extract_device_role ,
331
+ "platform" : self .extract_platform ,
332
+ "device_type" : self .extract_device_type ,
333
+ "services" : self .extract_services ,
334
+ "config_context" : self .extract_config_context ,
335
+ "manufacturer" : self .extract_manufacturer ,
336
+ "interfaces" : self .extract_interfaces ,
337
+ "custom_fields" : self .extract_custom_fields ,
338
+ }
339
+
340
+ def _pluralize (self , something ):
341
+ # If plurals is enabled, wrap in a single-element list for backwards compatibility
342
+ if self .plurals :
343
+ return [something ]
344
+ else :
345
+ return something
305
346
306
347
def extract_disk (self , host ):
307
348
return host .get ("disk" )
@@ -314,7 +355,7 @@ def extract_memory(self, host):
314
355
315
356
def extract_platform (self , host ):
316
357
try :
317
- return [ self .platforms_lookup [host ["platform" ]["id" ]]]
358
+ return self ._pluralize ( self . platforms_lookup [host ["platform" ]["id" ]])
318
359
except Exception :
319
360
return
320
361
@@ -333,48 +374,50 @@ def extract_services(self, host):
333
374
334
375
def extract_device_type (self , host ):
335
376
try :
336
- return [ self .device_types_lookup [host ["device_type" ]["id" ]]]
377
+ return self ._pluralize ( self . device_types_lookup [host ["device_type" ]["id" ]])
337
378
except Exception :
338
379
return
339
380
340
381
def extract_rack (self , host ):
341
382
try :
342
- return [ self .racks_lookup [host ["rack" ]["id" ]]]
383
+ return self ._pluralize ( self . racks_lookup [host ["rack" ]["id" ]])
343
384
except Exception :
344
385
return
345
386
346
387
def extract_site (self , host ):
347
388
try :
348
- return [ self .sites_lookup [host ["site" ]["id" ]]]
389
+ return self ._pluralize ( self . sites_lookup [host ["site" ]["id" ]])
349
390
except Exception :
350
391
return
351
392
352
393
def extract_tenant (self , host ):
353
394
try :
354
- return [ self .tenants_lookup [host ["tenant" ]["id" ]]]
395
+ return self ._pluralize ( self . tenants_lookup [host ["tenant" ]["id" ]])
355
396
except Exception :
356
397
return
357
398
358
399
def extract_device_role (self , host ):
359
400
try :
360
401
if "device_role" in host :
361
- return [self .device_roles_lookup [host ["device_role" ]["id" ]]]
402
+ return self ._pluralize (
403
+ self .device_roles_lookup [host ["device_role" ]["id" ]]
404
+ )
362
405
elif "role" in host :
363
- return [ self .device_roles_lookup [host ["role" ]["id" ]]]
406
+ return self ._pluralize ( self . device_roles_lookup [host ["role" ]["id" ]])
364
407
except Exception :
365
408
return
366
409
367
410
def extract_config_context (self , host ):
368
411
try :
369
- return [ host ["config_context" ]]
412
+ return self . _pluralize ( host ["config_context" ])
370
413
except Exception :
371
414
return
372
415
373
416
def extract_manufacturer (self , host ):
374
417
try :
375
- return [
418
+ return self . _pluralize (
376
419
self .manufacturers_lookup [host ["device_type" ]["manufacturer" ]["id" ]]
377
- ]
420
+ )
378
421
except Exception :
379
422
return
380
423
@@ -619,17 +662,18 @@ def extract_name(self, host):
619
662
return host ["name" ] or str (uuid .uuid4 ())
620
663
621
664
def add_host_to_groups (self , host , hostname ):
622
- for group in self .group_by :
623
- sub_groups = self .group_extractors [group ](host )
665
+ for grouping in self .group_by :
666
+ groups_for_host = self .group_extractors [grouping ](host )
624
667
625
- if not sub_groups :
668
+ if not groups_for_host :
626
669
continue
627
670
628
- for sub_group in sub_groups :
671
+ for group_for_host in groups_for_host :
629
672
if self .group_names_raw :
630
- group_name = sub_group
673
+ group_name = group_for_host
631
674
else :
632
- group_name = "_" .join ([group , sub_group ])
675
+ group_name = "_" .join ([grouping , group_for_host ])
676
+
633
677
self .inventory .add_group (group = group_name )
634
678
self .inventory .add_host (group = group_name , host = hostname )
635
679
@@ -693,6 +737,7 @@ def parse(self, inventory, loader, path, cache=True):
693
737
self .timeout = self .get_option ("timeout" )
694
738
self .validate_certs = self .get_option ("validate_certs" )
695
739
self .config_context = self .get_option ("config_context" )
740
+ self .plurals = self .get_option ("plurals" )
696
741
self .interfaces = self .get_option ("interfaces" )
697
742
self .services = self .get_option ("services" )
698
743
self .headers = {
0 commit comments