From 8a7a81904093862fc424c1040d3ad22d76bae4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 17 Oct 2018 22:28:20 +0200 Subject: [PATCH] Add support for platform grouping (#45733) --- lib/ansible/plugins/inventory/netbox.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ansible/plugins/inventory/netbox.py b/lib/ansible/plugins/inventory/netbox.py index 4b7f41720af..02efd9633d1 100644 --- a/lib/ansible/plugins/inventory/netbox.py +++ b/lib/ansible/plugins/inventory/netbox.py @@ -49,6 +49,7 @@ DOCUMENTATION = ''' - device_roles - device_types - manufacturers + - platforms default: [] query_filters: description: List of parameters passed to the query string (Multiple values may be separated by commas) @@ -194,6 +195,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable): "memory": self.extract_memory, "vcpus": self.extract_vcpus, "device_roles": self.extract_device_role, + "platforms": self.extract_platform, "device_types": self.extract_device_type, "manufacturers": self.extract_manufacturer } @@ -207,6 +209,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable): def extract_memory(self, host): return host.get("memory") + def extract_platform(self, host): + try: + return self.platforms_lookup[host["platform"]["id"]] + except Exception: + return + def extract_device_type(self, host): try: return [self.device_types_lookup[host["device_type"]["id"]]] @@ -267,6 +275,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable): def extract_tags(self, host): return host["tags"] + def refresh_platforms_lookup(self): + url = urljoin(self.api_endpoint, "/api/dcim/platforms/?limit=0") + platforms = self.get_resource_list(api_url=url) + self.platforms_lookup = dict((platform["id"], platform["name"]) for platform in platforms) + def refresh_sites_lookup(self): url = urljoin(self.api_endpoint, "/api/dcim/sites/?limit=0") sites = self.get_resource_list(api_url=url)