Add tags grouping (#45741)

This commit is contained in:
Rémy Léone 2018-10-06 17:06:39 +02:00 committed by ansibot
parent c3e5ebfa79
commit 9ff25c1383

View file

@ -43,6 +43,7 @@ DOCUMENTATION = '''
- sites - sites
- tenants - tenants
- racks - racks
- tags
- device_roles - device_roles
- device_types - device_types
- manufacturers - manufacturers
@ -172,6 +173,7 @@ class InventoryModule(BaseInventoryPlugin):
"sites": self.extract_site, "sites": self.extract_site,
"tenants": self.extract_tenant, "tenants": self.extract_tenant,
"racks": self.extract_rack, "racks": self.extract_rack,
"tags": self.extract_tags,
"device_roles": self.extract_device_role, "device_roles": self.extract_device_role,
"device_types": self.extract_device_type, "device_types": self.extract_device_type,
"manufacturers": self.extract_manufacturer "manufacturers": self.extract_manufacturer
@ -179,37 +181,37 @@ class InventoryModule(BaseInventoryPlugin):
def extract_device_type(self, host): def extract_device_type(self, host):
try: try:
return self.device_types_lookup[host["device_type"]["id"]] return [self.device_types_lookup[host["device_type"]["id"]]]
except Exception: except Exception:
return return
def extract_rack(self, host): def extract_rack(self, host):
try: try:
return self.racks_lookup[host["rack"]["id"]] return [self.racks_lookup[host["rack"]["id"]]]
except Exception: except Exception:
return return
def extract_site(self, host): def extract_site(self, host):
try: try:
return self.sites_lookup[host["site"]["id"]] return [self.sites_lookup[host["site"]["id"]]]
except Exception: except Exception:
return return
def extract_tenant(self, host): def extract_tenant(self, host):
try: try:
return self.tenants_lookup[host["tenant"]["id"]] return [self.tenants_lookup[host["tenant"]["id"]]]
except Exception: except Exception:
return return
def extract_device_role(self, host): def extract_device_role(self, host):
try: try:
return self.device_roles_lookup[host["device_role"]["id"]] return [self.device_roles_lookup[host["device_role"]["id"]]]
except Exception: except Exception:
return return
def extract_manufacturer(self, host): def extract_manufacturer(self, host):
try: try:
return self.manufacturers_lookup[host["device_type"]["manufacturer"]["id"]] return [self.manufacturers_lookup[host["device_type"]["manufacturer"]["id"]]]
except Exception: except Exception:
return return
@ -234,6 +236,9 @@ class InventoryModule(BaseInventoryPlugin):
except Exception: except Exception:
return return
def extract_tags(self, host):
return host["tags"]
def refresh_sites_lookup(self): def refresh_sites_lookup(self):
url = urljoin(self.api_endpoint, "/api/dcim/sites/?limit=0") url = urljoin(self.api_endpoint, "/api/dcim/sites/?limit=0")
sites = self.get_resource_list(api_url=url) sites = self.get_resource_list(api_url=url)
@ -318,13 +323,14 @@ class InventoryModule(BaseInventoryPlugin):
return host["name"] or str(uuid.uuid4()) return host["name"] or str(uuid.uuid4())
def add_host_to_groups(self, host, hostname): def add_host_to_groups(self, host, hostname):
for g in self.group_by: for group in self.group_by:
group = self.group_extractors[g](host) sub_groups = self.group_extractors[group](host)
if not group: if not sub_groups:
continue continue
group_name = "_".join([g, group]) for sub_group in sub_groups:
group_name = "_".join([group, sub_group])
self.inventory.add_group(group=group_name) self.inventory.add_group(group=group_name)
self.inventory.add_host(group=group_name, host=hostname) self.inventory.add_host(group=group_name, host=hostname)