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 prefix string to groups and hostvars output by nb_inventory plugin #984

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
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
45 changes: 39 additions & 6 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@
default: false
type: boolean
version_added: "0.2.0"
group_names_prefix:
description: Will add "netbox_" as a prefix to group names
default: False
type: boolean
version_added: "3.12.0"
host_vars_prefix:
description: Will add "netbox_" as a prefix to host variables
default: False
type: boolean
version_added: "3.12.0"
query_filters:
description:
- List of parameters passed to the query string for both devices and VMs (Multiple values may be separated by commas).
Expand Down Expand Up @@ -1799,9 +1809,14 @@ def generate_group_name(self, grouping, group):
group = group["value"]

if self.group_names_raw:
return group
group_name = group
else:
return "_".join([grouping, group])
group_name = "_".join([grouping, group])

if self.group_names_prefix:
group_name = "netbox_" + group_name

return group_name

def add_host_to_groups(self, host, hostname):
site_group_by = self._pluralize_group_by("site")
Expand Down Expand Up @@ -1950,11 +1965,19 @@ def _fill_host_variables(self, host, hostname):

extracted_primary_ip4 = self.extract_primary_ip4(host=host)
if extracted_primary_ip4:
self._set_variable(hostname, "primary_ip4", extracted_primary_ip4)
if self.host_vars_prefix:
hostvar = "netbox_primary_ipv4"
else:
hostvar = "primary_ipv4"
self._set_variable(hostname, hostvar, extracted_primary_ip4)

extracted_primary_ip6 = self.extract_primary_ip6(host=host)
if extracted_primary_ip6:
self._set_variable(hostname, "primary_ip6", extracted_primary_ip6)
if self.host_vars_prefix:
hostvar = "netbox_primary_ipv6"
else:
hostvar = "primary_ipv6"
self._set_variable(hostname, hostvar, extracted_primary_ip6)

extracted_oob_ip = self.extract_oob_ip(host=host)
if extracted_oob_ip:
Expand Down Expand Up @@ -1996,9 +2019,17 @@ def _fill_host_variables(self, host, hostname):
)
):
for key, value in extracted_value.items():
self._set_variable(hostname, key, value)
if self.host_vars_prefix:
hostvar = "netbox_" + key
else:
hostvar = key
self._set_variable(hostname, hostvar, value)
else:
self._set_variable(hostname, attribute, extracted_value)
if self.host_vars_prefix:
hostvar = "netbox_" + attribute
else:
hostvar = attribute
self._set_variable(hostname, hostvar, extracted_value)

def _get_host_virtual_chassis_master(self, host):
virtual_chassis = host.get("virtual_chassis", None)
Expand Down Expand Up @@ -2183,6 +2214,8 @@ def parse(self, inventory, loader, path, cache=True):
self.dns_name = self.get_option("dns_name")
self.ansible_host_dns_name = self.get_option("ansible_host_dns_name")
self.racks = self.get_option("racks")
self.group_names_prefix = self.get_option("group_names_prefix")
self.host_vars_prefix = self.get_option("host_vars_prefix")

# Compile regular expressions, if any
self.rename_variables = self.parse_rename_variables(
Expand Down
Loading