From 8965e736e08e126aee4ce29042b1258e66e8acfe Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Sun, 29 Dec 2024 20:35:01 -0500 Subject: [PATCH] fix gather tags --- plugins/inventory/esxi_hosts.py | 8 +++++--- plugins/inventory/vms.py | 7 ++++--- plugins/inventory_utils/_base.py | 26 +++++++++++++++----------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/plugins/inventory/esxi_hosts.py b/plugins/inventory/esxi_hosts.py index 477130d0..e274cbac 100644 --- a/plugins/inventory/esxi_hosts.py +++ b/plugins/inventory/esxi_hosts.py @@ -200,6 +200,10 @@ def management_ip(self): return self._management_ip + def get_tags(self, rest_client): + return rest_client.get_tags_by_host_moid(self.object._GetMoId()) + + class InventoryModule(VmwareInventoryBase): @@ -295,9 +299,7 @@ def populate_from_vcenter(self, config_data): ) if self.get_option("gather_tags"): - tags, tags_by_category = self.gather_tags(esxi_host.object._GetMoId()) - esxi_host.properties["tags"] = tags - esxi_host.properties["tags_by_category"] = tags_by_category + self.add_tags_to_object_properties(esxi_host) self.set_inventory_hostname(esxi_host) if esxi_host.inventory_hostname not in hostvars: diff --git a/plugins/inventory/vms.py b/plugins/inventory/vms.py index 5dc85989..f35aa150 100644 --- a/plugins/inventory/vms.py +++ b/plugins/inventory/vms.py @@ -180,6 +180,9 @@ def guest_ip(self): return self._guest_ip + def get_tags(self, rest_client): + return rest_client.get_tags_by_vm_moid(self.object._GetMoId()) + class InventoryModule(VmwareInventoryBase): @@ -272,9 +275,7 @@ def populate_from_vcenter(self, config_data): ) if self.get_option("gather_tags"): - tags, tags_by_category = self.gather_tags(vm.object._GetMoId()) - vm.properties["tags"] = tags - vm.properties["tags_by_category"] = tags_by_category + self.add_tags_to_object_properties(vm) self.set_inventory_hostname(vm) if vm.inventory_hostname not in hostvars: diff --git a/plugins/inventory_utils/_base.py b/plugins/inventory_utils/_base.py index 00971d99..c98b520f 100644 --- a/plugins/inventory_utils/_base.py +++ b/plugins/inventory_utils/_base.py @@ -6,6 +6,7 @@ __metaclass__ = type +from abc import ABC, abstractmethod from ansible.errors import AnsibleError from ansible.errors import AnsibleParserError from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable @@ -25,7 +26,7 @@ ) -class VmwareInventoryHost(): +class VmwareInventoryHost(ABC): def __init__(self): self.object = None self.inventory_hostname = None @@ -52,9 +53,13 @@ def create_from_object(cls, vmware_object, properties_to_gather, pyvmomi_client) host = cls() host.object = vmware_object host.path = get_folder_path_of_vsphere_object(vmware_object) - host.properties = host.set_properties_from_pyvmomi(properties_to_gather, pyvmomi_client) + host.properties = host.get_properties_from_pyvmomi(properties_to_gather, pyvmomi_client) return host + @abstractmethod + def get_tags(self, rest_client): + pass + def get_properties_from_pyvmomi(self, properties_to_gather, pyvmomi_client): properties = vmware_obj_to_json(self.object, properties_to_gather) properties['path'] = self.path @@ -217,23 +222,21 @@ def get_objects_by_type(self, vim_type): return objects - def gather_tags(self, object_moid): + def add_tags_to_object_properties(self, vmware_host_object): """ - Given an object moid, gather any tags attached to the object. + Given a subclass of VmwareInventoryHost object, gather any tags attached to the object and add them + to the properties. Also break the tags into the categories and add those to the objects properties. Args: - object_moid: str, The object's MOID + vmware_host_object: VmwareInventoryHost, A subclass instance of the VmwareInventoryHost class Returns: - tuple - First item is a dict with the object's tags. Keys are tag IDs and values are tag names - Second item is a dict of the object's tag categories. Keys are category names and values are a dict - containing the tags in the category + None """ if not hasattr(self, '_known_tag_category_ids_to_name'): self._known_tag_category_ids_to_name = {} tags = {} tags_by_category = {} - for tag in self.rest_client.get_tags_by_host_moid(object_moid): + for tag in vmware_host_object.get_tags(self.rest_client): tags[tag.id] = tag.name try: category_name = self._known_tag_category_ids_to_name[tag.category_id] @@ -246,7 +249,8 @@ def gather_tags(self, object_moid): tags_by_category[category_name].append({tag.id: tag.name}) - return tags, tags_by_category + vmware_host_object.properties['tags'] = tags + vmware_host_object.properties['tags_by_category'] = tags_by_category def set_inventory_hostname(self, vmware_host_object): """