Skip to content

Commit ad10114

Browse files
Inventory: Added interfaces and ip addresses (#98)
1 parent 689fe65 commit ad10114

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

plugins/inventory/netbox.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
# in order of precedence
4949
- name: NETBOX_TOKEN
5050
- name: NETBOX_API_KEY
51+
interfaces:
52+
description:
53+
- If True, it adds the device or virtual machine interface information in host vars.
54+
default: False
55+
type: boolean
56+
version_added: "0.1.7"
5157
group_by:
5258
description: Keys used to create groups.
5359
type: list
@@ -251,6 +257,7 @@ def group_extractors(self):
251257
"services": self.extract_services,
252258
"config_context": self.extract_config_context,
253259
"manufacturers": self.extract_manufacturer,
260+
"interfaces": self.extract_interfaces,
254261
}
255262

256263
def extract_disk(self, host):
@@ -347,6 +354,60 @@ def extract_primary_ip6(self, host):
347354
def extract_tags(self, host):
348355
return host["tags"]
349356

357+
def extract_ipaddresses(self, host):
358+
try:
359+
if self.interfaces:
360+
if "device_role" in host:
361+
url = (
362+
self.api_endpoint
363+
+ "/api/ipam/ip-addresses/?limit=0&device_id=%s"
364+
% (to_text(host["id"]))
365+
)
366+
elif "role" in host:
367+
url = (
368+
self.api_endpoint
369+
+ "/api/ipam/ip-addresses/?limit=0&virtual_machine_id=%s"
370+
% (to_text(host["id"]))
371+
)
372+
ipaddress_lookup = self.get_resource_list(api_url=url)
373+
374+
return ipaddress_lookup
375+
except Exception:
376+
return
377+
378+
def extract_interfaces(self, host):
379+
try:
380+
if self.interfaces:
381+
if "device_role" in host:
382+
url = (
383+
self.api_endpoint
384+
+ "/api/dcim/interfaces/?limit=0&device_id=%s"
385+
% (to_text(host["id"]))
386+
)
387+
elif "role" in host:
388+
url = (
389+
self.api_endpoint
390+
+ "/api/virtualization/interfaces/?limit=0&virtual_machine_id=%s"
391+
% (to_text(host["id"]))
392+
)
393+
interface_lookup = self.get_resource_list(api_url=url)
394+
395+
# Collect all IP Addresses associated with the device
396+
device_ipaddresses = self.extract_ipaddresses(host)
397+
398+
# Attach the found IP Addresses record to the interface
399+
for interface in interface_lookup:
400+
interface_ip = [
401+
ipaddress
402+
for ipaddress in device_ipaddresses
403+
if ipaddress["interface"]["id"] == interface["id"]
404+
]
405+
interface["ip-addresses"] = interface_ip
406+
407+
return interface_lookup
408+
except Exception:
409+
return
410+
350411
def refresh_platforms_lookup(self):
351412
url = self.api_endpoint + "/api/dcim/platforms/?limit=0"
352413
platforms = self.get_resource_list(api_url=url)
@@ -553,6 +614,7 @@ def parse(self, inventory, loader, path, cache=True):
553614
self.timeout = self.get_option("timeout")
554615
self.validate_certs = self.get_option("validate_certs")
555616
self.config_context = self.get_option("config_context")
617+
self.interfaces = self.get_option("interfaces")
556618
self.headers = {
557619
"Authorization": "Token %s" % token,
558620
"User-Agent": "ansible %s Python %s"

0 commit comments

Comments
 (0)