Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add computed_fields inventory plugin #334

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions plugins/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Nikhil Singh Baliyan (@nikkytub)
- Sander Steffann (@steffann)
- Douglas Heriot (@DouglasHeriot)
- Yannis Ansermoz (@Yannis100)
short_description: Nautobot inventory source
description:
- Get inventory hosts from Nautobot
Expand Down Expand Up @@ -65,6 +66,15 @@
default: False
type: boolean
version_added: "1.0.0"
computed_fields:
description:
- If True, it adds computed_fields in host vars.
default: False
type: boolean
flatten_computed_fields:
description:
- If I(computed_fields) is enabled, by default it's added as a host var named computed_fields.
- If flatten_computed_fields is set to True, the computed fields variables will be added directly to the host instead.
flatten_custom_fields:
description:
- By default, host custom fields are added as a dictionary host var named custom_fields.
Expand Down Expand Up @@ -1105,6 +1115,13 @@ def refresh_url(self):
if vm_url:
vm_url = f"{vm_url}&include=config_context"

# Include computed_fields if required
if self.computed_fields:
if device_url:
device_url = f"{device_url}&include=computed_fields"
if vm_url:
vm_url = f"{vm_url}&include=computed_fields"

return device_url, vm_url

def fetch_hosts(self):
Expand Down Expand Up @@ -1255,6 +1272,7 @@ def _fill_host_variables(self, host, hostname):
(attribute == "config_context" and self.flatten_config_context)
or (attribute == "custom_fields" and self.flatten_custom_fields)
or (attribute == "local_config_context_data" and self.flatten_local_context_data)
or (attribute == "computed_fields" and self.flatten_computed_fields)
):
for key, value in extracted_value.items():
self.inventory.set_variable(hostname, key, value)
Expand Down Expand Up @@ -1333,6 +1351,8 @@ def parse(self, inventory, loader, path, cache=True):
self.config_context = self.get_option("config_context")
self.flatten_config_context = self.get_option("flatten_config_context")
self.flatten_local_context_data = self.get_option("flatten_local_context_data")
self.computed_fields = self.get_option("computed_fields")
self.flatten_computed_fields = self.get_option("flatten_computed_fields")
self.flatten_custom_fields = self.get_option("flatten_custom_fields")
self.plurals = self.get_option("plurals")
self.interfaces = self.get_option("interfaces")
Expand Down
Loading