'Reads the inventory from cache or VMware API via pSphere.'

def get_inventory(self, meta_hostvars=True):

    if self.guests_only:
        cache_name = '__inventory_guests__'
    else:
        cache_name = '__inventory_all__'
    inv = self._get_cache(cache_name, None)
    if (inv is not None):
        return inv
    inv = {'all': {'hosts': []}}
    if meta_hostvars:
        inv['_meta'] = {'hostvars': {}}
    default_group = os.path.basename(sys.argv[0]).rstrip('.py')
    if (not self.guests_only):
        if self.config.has_option('defaults', 'hw_group'):
            hw_group = self.config.get('defaults', 'hw_group')
        else:
            hw_group = (default_group + '_hw')
    if self.config.has_option('defaults', 'vm_group'):
        vm_group = self.config.get('defaults', 'vm_group')
    else:
        vm_group = (default_group + '_vm')
    if self.config.has_option('defaults', 'prefix_filter'):
        prefix_filter = self.config.get('defaults', 'prefix_filter')
    else:
        prefix_filter = None
    if self.filter_clusters:
        hosts = []
        for cluster in ClusterComputeResource.all(self.client):
            if (cluster.name in self.filter_clusters):
                for host in cluster.host:
                    hosts.append(host)
    else:
        hosts = HostSystem.all(self.client)
    for host in hosts:
        if (not self.guests_only):
            self._add_host(inv, 'all', host.name)
            self._add_host(inv, hw_group, host.name)
            host_info = self._get_host_info(host)
            if meta_hostvars:
                inv['_meta']['hostvars'][host.name] = host_info
            self._put_cache(host.name, host_info)
        for vm in host.vm:
            if prefix_filter:
                if vm.name.startswith(prefix_filter):
                    continue
            self._add_host(inv, 'all', vm.name)
            self._add_host(inv, vm_group, vm.name)
            vm_info = self._get_vm_info(vm)
            if meta_hostvars:
                inv['_meta']['hostvars'][vm.name] = vm_info
            self._put_cache(vm.name, vm_info)
            vm_resourcePool = vm_info.get('vmware_resourcePool', None)
            if vm_resourcePool:
                self._add_child(inv, vm_group, 'resource_pools')
                self._add_child(inv, 'resource_pools', vm_resourcePool)
                self._add_host(inv, vm_resourcePool, vm.name)
            for vm_datastore in vm_info.get('vmware_datastores', []):
                self._add_child(inv, vm_group, 'datastores')
                self._add_child(inv, 'datastores', vm_datastore)
                self._add_host(inv, vm_datastore, vm.name)
            for vm_network in vm_info.get('vmware_networks', []):
                self._add_child(inv, vm_group, 'networks')
                self._add_child(inv, 'networks', vm_network)
                self._add_host(inv, vm_network, vm.name)
            vm_guestId = vm_info.get('vmware_guestId', None)
            if vm_guestId:
                self._add_child(inv, vm_group, 'guests')
                self._add_child(inv, 'guests', vm_guestId)
                self._add_host(inv, vm_guestId, vm.name)
            vm_template = vm_info.get('vmware_template', False)
            if vm_template:
                self._add_child(inv, vm_group, 'templates')
                self._add_host(inv, 'templates', vm.name)
    self._put_cache(cache_name, inv)
    return inv
