# Buggy code
def _populate_from_source(self, source_data, using_current_cache):
    """
    Populate inventory data from direct source

    """
    if using_current_cache:
        self._populate_from_cache(source_data)
        return source_data

    cacheable_results = {}
    hostvars = {}
    objects = self._get_managed_objects_properties(
        vim_type=vim.VirtualMachine, properties=["name"]
    )

    if self.with_tags:
        tag_svc = Tag(self.rest_content)
        tag_association = TagAssociation(self.rest_content)

        tags_info = dict()
        tags = tag_svc.list()
        for tag in tags:
            tag_obj = tag_svc.get(tag)
            tags_info[tag_obj.id] = tag_obj.name
            if tag_obj.name not in cacheable_results:
                cacheable_results[tag_obj.name] = {"hosts": []}
                self.inventory.add_group(tag_obj.name)

    for temp_vm_object in objects:
        for temp_vm_object_property in temp_vm_object.propSet:
            # VMware does not provide a way to uniquely identify VM by its name
            # i.e. there can be two virtual machines with same name
            # Appending "_" and VMware UUID to make it unique
            current_host = (
                temp_vm_object_property.val + "_" + temp_vm_object.obj.config.uuid
            )

            if current_host not in hostvars:
                hostvars[current_host] = {}
                self.inventory.add_host(current_host)
                host_ip = temp_vm_object.obj.guest.ipAddress
                if host_ip:
                    self.inventory.set_variable(current_host, "ansible_host", host_ip)

                # Load VM properties in host_vars
                vm_properties = [
                    "name",
                    "config.cpuHotAddEnabled",
                    "config.cpuHotRemoveEnabled",
                    "config.instanceUuid",
                    "config.hardware.numCPU",
                    "config.template",
                    "config.name",
                    "guest.hostName",
                    "guest.ipAddress",
                    "guest.guestId",
                    "guest.guestState",
                    "runtime.maxMemoryUsage",
                    "customValue",
                ]
                for vm_prop in vm_properties:
                    vm_value = self._get_vm_prop(temp_vm_object.obj, vm_prop.split("."))
                    self.inventory.set_variable(current_host, vm_prop, vm_value)
                # Only gather facts related to tag if vCloud and vSphere is installed.
                if HAS_VCLOUD and HAS_VSPHERE and self.with_tags:
                    # Add virtual machine to appropriate tag group
                    vm_mo_id = temp_vm_object.obj._GetMoId()
                    vm_dynamic_id = DynamicID(type="VirtualMachine", id=vm_mo_id)
                    attached_tags = tag_association.list_attached_tags(vm_dynamic_id)

                    for tag_id in attached_tags:
                        self.inventory.add_child(tags_info[tag_id], current_host)
                        cacheable_results[tags_info[tag_id]]["hosts"].append(
                            current_host
                        )

                # Based on power state of virtual machine
                vm_power = temp_vm_object.obj.summary.runtime.powerState
                if vm_power not in cacheable_results:
                    cacheable_results[vm_power] = []
                    self.inventory.add_group(vm_power)
                cacheable_results[vm_power].append(current_host)
                self.inventory.add_child(vm_power, current_host)

                # Based on guest id
                vm_guest_id = temp_vm_object.obj.config.guestId
                if vm_guest_id and vm_guest_id not in cacheable_results:
                    cacheable_results[vm_guest_id] = []
                    self.inventory.add_group(vm_guest_id)
                cacheable_results[vm_guest_id].append(current_host)
                self.inventory.add_child(vm_guest_id, current_host)

    return cacheable_results


# Traceback (RepresenterError)
ansible-inventory --list --yaml -i inventory.vmware.yml -vvvv
ansible-inventory 2.7.7
config file = /mnt/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible-inventory
python version = 2.7.15 (default, Feb  6 2019, 12:35:55) [GCC 6.3.0 20170516]
Using /mnt/ansible/ansible.cfg as config file
setting up inventory plugins
/mnt/ansible/inventory.vmware.yml did not meet host_list requirements, check plugin documentation if this is unexpected
/mnt/ansible/inventory.vmware.yml did not meet script requirements, check plugin documentation if this is unexpected
Parsed /mnt/ansible/inventory.vmware.yml inventory source with auto plugin
ERROR! Unexpected Exception, this is probably a bug: cannot represent an object: (vim.CustomFieldsManager.Value) []
the full traceback was:

Traceback (most recent call last):
File "/usr/local/bin/ansible-inventory", line 118, in <module>
exit_code = cli.run()
File "/usr/local/lib/python2.7/site-packages/ansible/cli/inventory.py", line 181, in run
results = self.dump(results)
File "/usr/local/lib/python2.7/site-packages/ansible/cli/inventory.py", line 195, in dump
results = yaml.dump(stuff, Dumper=AnsibleDumper, default_flow_style=False)
File "/usr/local/lib/python2.7/site-packages/yaml/__init__.py", line 202, in dump
return dump_all([data], stream, Dumper=Dumper, **kwds)
File "/usr/local/lib/python2.7/site-packages/yaml/__init__.py", line 190, in dump_all
dumper.represent(data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 28, in represent
node = self.represent_data(data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 67, in represent_data
node = self.yaml_representers[None](self, data)
File "/usr/local/lib/python2.7/site-packages/yaml/representer.py", line 249, in represent_undefined
raise RepresenterError("cannot represent an object: %s" % data)
RepresenterError: cannot represent an object: (vim.CustomFieldsManager.Value) []

# Fixed code
def _populate_from_source(self, source_data, using_current_cache):
    """
    Populate inventory data from direct source

    """
    if using_current_cache:
        self._populate_from_cache(source_data)
        return source_data

    cacheable_results = {"_meta": {"hostvars": {}}}
    hostvars = {}
    objects = self.pyv._get_managed_objects_properties(
        vim_type=vim.VirtualMachine, properties=["name"]
    )

    if self.pyv.with_tags:
        tag_svc = Tag(self.pyv.rest_content)
        tag_association = TagAssociation(self.pyv.rest_content)

        tags_info = dict()
        tags = tag_svc.list()
        for tag in tags:
            tag_obj = tag_svc.get(tag)
            tags_info[tag_obj.id] = tag_obj.name
            if tag_obj.name not in cacheable_results:
                cacheable_results[tag_obj.name] = {"hosts": []}
                self.inventory.add_group(tag_obj.name)

    for vm_obj in objects:
        for vm_obj_property in vm_obj.propSet:
            # VMware does not provide a way to uniquely identify VM by its name
            # i.e. there can be two virtual machines with same name
            # Appending "_" and VMware UUID to make it unique
            current_host = vm_obj_property.val + "_" + vm_obj.obj.config.uuid

            if current_host not in hostvars:
                hostvars[current_host] = {}
                self.inventory.add_host(current_host)

                host_ip = vm_obj.obj.guest.ipAddress
                if host_ip:
                    self.inventory.set_variable(current_host, "ansible_host", host_ip)

                self._populate_host_properties(vm_obj, current_host)

                # Only gather facts related to tag if vCloud and vSphere is installed.
                if HAS_VCLOUD and HAS_VSPHERE and self.pyv.with_tags:
                    # Add virtual machine to appropriate tag group
                    vm_mo_id = vm_obj.obj._GetMoId()
                    vm_dynamic_id = DynamicID(type="VirtualMachine", id=vm_mo_id)
                    attached_tags = tag_association.list_attached_tags(vm_dynamic_id)

                    for tag_id in attached_tags:
                        self.inventory.add_child(tags_info[tag_id], current_host)
                        cacheable_results[tags_info[tag_id]]["hosts"].append(
                            current_host
                        )

                # Based on power state of virtual machine
                vm_power = str(vm_obj.obj.summary.runtime.powerState)
                if vm_power not in cacheable_results:
                    cacheable_results[vm_power] = {"hosts": []}
                    self.inventory.add_group(vm_power)
                cacheable_results[vm_power]["hosts"].append(current_host)
                self.inventory.add_child(vm_power, current_host)

                # Based on guest id
                vm_guest_id = vm_obj.obj.config.guestId
                if vm_guest_id and vm_guest_id not in cacheable_results:
                    cacheable_results[vm_guest_id] = {"hosts": []}
                    self.inventory.add_group(vm_guest_id)
                cacheable_results[vm_guest_id]["hosts"].append(current_host)
                self.inventory.add_child(vm_guest_id, current_host)

    for host in hostvars:
        h = self.inventory.get_host(host)
        cacheable_results["_meta"]["hostvars"][h.name] = h.vars

    return cacheable_results