Skip to content

Commit

Permalink
fix gather tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Dec 30, 2024
1 parent a866639 commit 8965e73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
8 changes: 5 additions & 3 deletions plugins/inventory/esxi_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions plugins/inventory/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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:
Expand Down
26 changes: 15 additions & 11 deletions plugins/inventory_utils/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +26,7 @@
)


class VmwareInventoryHost():
class VmwareInventoryHost(ABC):
def __init__(self):
self.object = None
self.inventory_hostname = None
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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):
"""
Expand Down

0 comments on commit 8965e73

Please sign in to comment.