# Buggy code
def get_all_objs(self, content, types, confine_to_datacenter=True):
    """Wrapper around get_all_objs to set datacenter context"""
    objects = get_all_objs(content, types)
    if confine_to_datacenter:
        if hasattr(objects, "items"):
            # resource pools come back as a dictionary
            for k, v in objects.items():
                parent_dc = get_parent_datacenter(k)
                if parent_dc.name != self.dc_name:
                    objects.pop(k, None)
        else:
            # everything else should be a list
            objects = [
                x for x in objects if get_parent_datacenter(x).name == self.dc_name
            ]

    return objects


# Traceback (nRuntimeError)
TASK [vmware_datastore_facts : get list of facts about datastores] *******************************************************************************
fatal: [testhost]: FAILED! => {"changed": false, "module_stderr": "/home/goneri/.ansible/tmp/ansible-tmp-1554391374.960104-37456948197302/AnsiballZ_vmware_datastore_facts.py:32: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses\n  import imp\nTraceback (most recent call last):\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1554391374.960104-37456948197302/AnsiballZ_vmware_datastore_facts.py\", line 128, in <module>\n    _ansiballz_main()\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1554391374.960104-37456948197302/AnsiballZ_vmware_datastore_facts.py\", line 120, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/goneri/.ansible/tmp/ansible-tmp-1554391374.960104-37456948197302/AnsiballZ_vmware_datastore_facts.py\", line 63, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/home/goneri/.virtualenvs/ansible/lib64/python3.7/imp.py\", line 234, in load_module\n    return load_source(name, filename, file)\n  File \"/home/goneri/.virtualenvs/ansible/lib64/python3.7/imp.py\", line 169, in load_source\n    module = _exec(spec, sys.modules[name])\n  File \"<frozen importlib._bootstrap>\", line 630, in _exec\n  File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n  File \"/tmp/ansible_vmware_datastore_facts_payload_4xcpbipq/__main__.py\", line 290, in <module>\n  File \"/tmp/ansible_vmware_datastore_facts_payload_4xcpbipq/__main__.py\", line 271, in main\n  File \"/tmp/ansible_vmware_datastore_facts_payload_4xcpbipq/__main__.py\", line 236, in lookup_datastore\n  File \"/tmp/ansible_vmware_datastore_facts_payload_4xcpbipq/__main__.py\", line 217, in get_all_objs\nRuntimeError: dictionary changed size during iteration\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

# Fixed code
def get_all_objs(self, content, types, confine_to_datacenter=True):
    """Wrapper around get_all_objs to set datacenter context"""
    objects = get_all_objs(content, types)
    if confine_to_datacenter:
        if hasattr(objects, "items"):
            # resource pools come back as a dictionary
            tmpobjs = objects.copy()
            for k, v in objects.items():
                parent_dc = get_parent_datacenter(k)
                if parent_dc.name != self.dc_name:
                    tmpobjs.pop(k, None)
            objects = tmpobjs
        else:
            # everything else should be a list
            objects = [
                x for x in objects if get_parent_datacenter(x).name == self.dc_name
            ]

    return objects