|
48 | 48 | # in order of precedence
|
49 | 49 | - name: NETBOX_TOKEN
|
50 | 50 | - 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" |
51 | 57 | group_by:
|
52 | 58 | description: Keys used to create groups.
|
53 | 59 | type: list
|
@@ -251,6 +257,7 @@ def group_extractors(self):
|
251 | 257 | "services": self.extract_services,
|
252 | 258 | "config_context": self.extract_config_context,
|
253 | 259 | "manufacturers": self.extract_manufacturer,
|
| 260 | + "interfaces": self.extract_interfaces, |
254 | 261 | }
|
255 | 262 |
|
256 | 263 | def extract_disk(self, host):
|
@@ -347,6 +354,60 @@ def extract_primary_ip6(self, host):
|
347 | 354 | def extract_tags(self, host):
|
348 | 355 | return host["tags"]
|
349 | 356 |
|
| 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 | + |
350 | 411 | def refresh_platforms_lookup(self):
|
351 | 412 | url = self.api_endpoint + "/api/dcim/platforms/?limit=0"
|
352 | 413 | platforms = self.get_resource_list(api_url=url)
|
@@ -553,6 +614,7 @@ def parse(self, inventory, loader, path, cache=True):
|
553 | 614 | self.timeout = self.get_option("timeout")
|
554 | 615 | self.validate_certs = self.get_option("validate_certs")
|
555 | 616 | self.config_context = self.get_option("config_context")
|
| 617 | + self.interfaces = self.get_option("interfaces") |
556 | 618 | self.headers = {
|
557 | 619 | "Authorization": "Token %s" % token,
|
558 | 620 | "User-Agent": "ansible %s Python %s"
|
|
0 commit comments