Skip to content

Commit 6ae20ee

Browse files
authored
nb_inventory: Add an option bypass querying racks (#701)
1 parent 1da0fc2 commit 6ae20ee

File tree

9 files changed

+3720
-18
lines changed

9 files changed

+3720
-18
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
version_added: "0.2.1"
134134
group_by:
135135
description:
136-
- Keys used to create groups. The I(plurals) option controls which of these are valid.
136+
- Keys used to create groups. The I(plurals) and I(racks) options control which of these are valid.
137137
- I(rack_group) is supported on NetBox versions 2.10 or lower only
138138
- I(location) is supported on NetBox versions 2.11 or higher only
139139
type: list
@@ -214,6 +214,12 @@
214214
description: List of custom ansible host vars to create from the device object fetched from NetBox
215215
default: {}
216216
type: dict
217+
racks:
218+
description:
219+
- If False, skip querying the racks for information, which can be slow with great amounts of racks.
220+
- The choices of I(group_by) will be changed by this option.
221+
type: boolean
222+
default: True
217223
"""
218224

219225
EXAMPLES = """
@@ -466,29 +472,35 @@ def group_extractors(self):
466472
"is_virtual": self.extract_is_virtual,
467473
self._pluralize_group_by("site"): self.extract_site,
468474
self._pluralize_group_by("tenant"): self.extract_tenant,
469-
self._pluralize_group_by("rack"): self.extract_rack,
470-
"rack_role": self.extract_rack_role,
471475
self._pluralize_group_by("tag"): self.extract_tags,
472476
self._pluralize_group_by("role"): self.extract_device_role,
473477
self._pluralize_group_by("platform"): self.extract_platform,
474478
self._pluralize_group_by("device_type"): self.extract_device_type,
475479
self._pluralize_group_by("manufacturer"): self.extract_manufacturer,
476480
}
477481

478-
# Locations were added in 2.11 replacing rack-groups.
479-
if self.api_version >= version.parse("2.11"):
482+
if self.racks:
480483
extractors.update(
481484
{
482-
"location": self.extract_location,
483-
}
484-
)
485-
else:
486-
extractors.update(
487-
{
488-
"rack_group": self.extract_rack_group,
485+
self._pluralize_group_by("rack"): self.extract_rack,
486+
"rack_role": self.extract_rack_role,
489487
}
490488
)
491489

490+
# Locations were added in 2.11 replacing rack-groups.
491+
if self.api_version >= version.parse("2.11"):
492+
extractors.update(
493+
{
494+
"location": self.extract_location,
495+
}
496+
)
497+
else:
498+
extractors.update(
499+
{
500+
"rack_group": self.extract_rack_group,
501+
}
502+
)
503+
492504
if self.services:
493505
extractors.update(
494506
{
@@ -1226,8 +1238,6 @@ def lookup_processes(self):
12261238
self.refresh_regions_lookup,
12271239
self.refresh_locations_lookup,
12281240
self.refresh_tenants_lookup,
1229-
self.refresh_racks_lookup,
1230-
self.refresh_rack_groups_lookup,
12311241
self.refresh_device_roles_lookup,
12321242
self.refresh_platforms_lookup,
12331243
self.refresh_device_types_lookup,
@@ -1244,6 +1254,14 @@ def lookup_processes(self):
12441254
if self.services:
12451255
lookups.append(self.refresh_services)
12461256

1257+
if self.racks:
1258+
lookups.extend(
1259+
[
1260+
self.refresh_racks_lookup,
1261+
self.refresh_rack_groups_lookup,
1262+
]
1263+
)
1264+
12471265
return lookups
12481266

12491267
@property
@@ -1494,7 +1512,11 @@ def add_host_to_groups(self, host, hostname):
14941512

14951513
if grouping not in self.group_extractors:
14961514
raise AnsibleError(
1497-
'group_by option "%s" is not valid. Check group_by documentation or check the plurals option. It can determine what group_by options are valid.'
1515+
(
1516+
'group_by option "%s" is not valid.'
1517+
" Check group_by documentation or check the plurals option, as well as the racks options."
1518+
" It can determine what group_by options are valid."
1519+
)
14981520
% grouping
14991521
)
15001522

@@ -1782,5 +1804,6 @@ def parse(self, inventory, loader, path, cache=True):
17821804
self.virtual_chassis_name = self.get_option("virtual_chassis_name")
17831805
self.dns_name = self.get_option("dns_name")
17841806
self.ansible_host_dns_name = self.get_option("ansible_host_dns_name")
1807+
self.racks = self.get_option("racks")
17851808

17861809
self.main()

0 commit comments

Comments
 (0)