Skip to content

Commit

Permalink
Merge pull request #352 from nautobot/add_ipv6_ansible_host
Browse files Browse the repository at this point in the history
Add Capability to Support IPv6 Host in Primary IP
  • Loading branch information
jvanderaa authored Jul 16, 2024
2 parents f5a093d + 30d7565 commit 900395a
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 199 deletions.
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, device, default_ip_version="ipv4"):
"""Add primary IP address to host."""
# Check to see what the primary IP host addition is, first case is IPv4, which is the default
order_of_preference = ["primary_ip4"]

# if default_ip_version is IPv4, prepend, else add to the end
if default_ip_version.lower() == "ipv6":
order_of_preference.insert(0, "primary_ip6")
else:
self.add_variable(device["name"], device["name"], "ansible_host")
order_of_preference.append("primary_ip6")

# 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 address_type in device and device[address_type].get("host"):
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(device, self.default_ip_version)
self.add_ansible_platform(device)
self.populate_variables(device)
self.create_groups(device)
Expand Down
Loading

0 comments on commit 900395a

Please sign in to comment.