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 Capability to Support IPv6 Host in Primary IP #352

Merged
merged 11 commits into from
Jul 16, 2024
56 changes: 46 additions & 10 deletions plugins/inventory/gql_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
env:
# in order of precedence
- name: NAUTOBOT_TOKEN
default_ip_version:
required: False
description:
- Choice between IPv6 and IPv4 address as the primary IP for ansible_host.
choices: ["IPv4", "ipv4", "IPv6", "ipv6"]
default: "IPv4"
query:
required: False
description:
Expand Down Expand Up @@ -141,6 +147,24 @@
tags: name
tenant: name

# Add the default IP version to be used for the ansible_host
plugin: networktocode.nautobot.gql_inventory
api_endpoint: http://localhost:8000
default_ip_version: ipv6
query:
devices:
tags: name
serial:
tenant: name
location:
name:
contact_name:
description:
parent: name
virtual_machines:
tags: name
tenant: name

# To group by use group_by key
# Specify the full path to the data you would like to use to group by.
# Ensure all paths are also included in the query.
Expand Down Expand Up @@ -249,16 +273,26 @@ def add_variable(self, host: str, var: str, var_type: str):
"""
self.inventory.set_variable(host, var_type, var)

def add_ipv4_address(self, device):
"""Add primary IPv4 address to host."""
if device["primary_ip4"]:
if not device["primary_ip4"].get("host"):
self.display.error("Mapping ansible_host requires primary_ip4.host as part of the query.")
self.add_variable(device["name"], device["name"], "ansible_host")
return
self.add_variable(device["name"], device["primary_ip4"]["host"], "ansible_host")
def add_ip_address(self, default_ip_version, device):
"""Add primary IP address to host."""
# Check to see what the primary IP host addition is, first case is IPv6
order_of_preference = ["primary_ip6"]

# if default_ip_version is IPv4, prepend, else postpend
if default_ip_version.lower() == "ipv4":
order_of_preference.insert(0, "primary_ip4")
else:
self.add_variable(device["name"], device["name"], "ansible_host")
order_of_preference.append("primary_ip4")

# Check of the address types in the order preference and if it find the first one, add that primary IP to the host
for address_type in order_of_preference:
if device.get(address_type, {}).get("host"):
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
self.add_variable(device["name"], device[address_type]["host"], "ansible_host")
return

# No return found, providing a mapping to just the device name
self.display.error("Mapping ansible_host requires primary_ip6.host or primary_ip4.host as part of the query.")
self.add_variable(device["name"], device["name"], "ansible_host")

def add_ansible_platform(self, device):
"""Add network platform to host"""
Expand Down Expand Up @@ -349,11 +383,13 @@ def main(self):
"devices": {
"name": None,
"primary_ip4": "host",
"primary_ip6": "host",
"platform": "napalm_driver",
},
"virtual_machines": {
"name": None,
"primary_ip4": "host",
"primary_ip6": "host",
"platform": "name",
},
}
Expand Down Expand Up @@ -388,7 +424,7 @@ def main(self):
for device in json_data["data"].get("devices", []) + json_data["data"].get("virtual_machines", []):
hostname = device["name"]
self.inventory.add_host(host=hostname)
self.add_ipv4_address(device)
self.add_ip_address(self.default_ip_version, device)
self.add_ansible_platform(device)
self.populate_variables(device)
self.create_groups(device)
Expand Down
Loading