Skip to content

Inventory-feature: Added interfaces and ip addresses #98

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

Merged
merged 1 commit into from
Feb 20, 2020
Merged
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
62 changes: 62 additions & 0 deletions plugins/inventory/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
# in order of precedence
- name: NETBOX_TOKEN
- name: NETBOX_API_KEY
interfaces:
description:
- If True, it adds the device or virtual machine interface information in host vars.
default: False
type: boolean
version_added: "0.1.7"
group_by:
description: Keys used to create groups.
type: list
Expand Down Expand Up @@ -250,6 +256,7 @@ def group_extractors(self):
"device_types": self.extract_device_type,
"config_context": self.extract_config_context,
"manufacturers": self.extract_manufacturer,
"interfaces": self.extract_interfaces,
}

def extract_disk(self, host):
Expand Down Expand Up @@ -338,6 +345,60 @@ def extract_primary_ip6(self, host):
def extract_tags(self, host):
return host["tags"]

def extract_ipaddresses(self, host):
try:
if self.interfaces:
if "device_role" in host:
url = (
self.api_endpoint
+ "/api/ipam/ip-addresses/?limit=0&device_id=%s"
% (to_text(host["id"]))
)
elif "role" in host:
url = (
self.api_endpoint
+ "/api/ipam/ip-addresses/?limit=0&virtual_machine_id=%s"
% (to_text(host["id"]))
)
ipaddress_lookup = self.get_resource_list(api_url=url)

return ipaddress_lookup
except Exception:
return

def extract_interfaces(self, host):
try:
if self.interfaces:
if "device_role" in host:
url = (
self.api_endpoint
+ "/api/dcim/interfaces/?limit=0&device_id=%s"
% (to_text(host["id"]))
)
elif "role" in host:
url = (
self.api_endpoint
+ "/api/virtualization/interfaces/?limit=0&virtual_machine_id=%s"
% (to_text(host["id"]))
)
interface_lookup = self.get_resource_list(api_url=url)

# Collect all IP Addresses associated with the device
device_ipaddresses = self.extract_ipaddresses(host)

# Attach the found IP Addresses record to the interface
for interface in interface_lookup:
interface_ip = [
ipaddress
for ipaddress in device_ipaddresses
if ipaddress["interface"]["id"] == interface["id"]
]
interface["ip-addresses"] = interface_ip

return interface_lookup
except Exception:
return

def refresh_platforms_lookup(self):
url = self.api_endpoint + "/api/dcim/platforms/?limit=0"
platforms = self.get_resource_list(api_url=url)
Expand Down Expand Up @@ -542,6 +603,7 @@ def parse(self, inventory, loader, path, cache=True):
self.timeout = self.get_option("timeout")
self.validate_certs = self.get_option("validate_certs")
self.config_context = self.get_option("config_context")
self.interfaces = self.get_option("interfaces")
self.headers = {
"Authorization": "Token %s" % token,
"User-Agent": "ansible %s Python %s"
Expand Down