# Buggy code
def select_resource_pool(self, host):
    resource_pools = get_all_objs(self.content, [vim.ResourcePool])
    for rp in resource_pools.items():
        if not rp[0]:
            continue

        if not hasattr(rp[0], "parent"):
            continue

        # Find resource pool on host
        if self.obj_has_parent(rp[0].parent, host.parent):
            # If no resource_pool selected or it's the selected pool, return it
            if (
                self.module.params["resource_pool"] is None
                or rp[0].name == self.module.params["resource_pool"]
            ):
                return rp[0]

    if self.module.params["resource_pool"] is not None:
        self.module.fail_json(
            msg="Could not find resource_pool %s for selected host %s"
            % (self.module.params["resource_pool"], host.name)
        )
    else:
        self.module.fail_json(msg="Failed to find a resource group for %s" % host.name)


# Traceback (nAssertionError)
TASK [Create fresh VM] ********************************************************************************
fatal: [basevm-centos72.ose.bld.f4tech.com -> localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:852
: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  Insecur
eRequestWarning)\nTraceback (most recent call last):\n  File \"/tmp/ansible_XHEfTR/ansible_module_vmware_guest.py\", line 1348, in <module>\n    main()\n  File \"/tmp/ansible_XHEfTR/ansible_module_vmware_gues
t.py\", line 1337, in main\n    result = pyv.deploy_vm()\n  File \"/tmp/ansible_XHEfTR/ansible_module_vmware_guest.py\", line 1079, in deploy_vm\n    resource_pool = self.select_resource_pool(hostsystem)\n  F
ile \"/tmp/ansible_XHEfTR/ansible_module_vmware_guest.py\", line 1018, in select_resource_pool\n    if self.obj_has_parent(rp[0].parent, host.parent):\n  File \"/tmp/ansible_XHEfTR/ansible_module_vmware_guest
.py\", line 983, in obj_has_parent\n    assert obj is not None and parent is not None\nAssertionError\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}

# Fixed code
def select_resource_pool(self, host):
    resource_pools = get_all_objs(self.content, [vim.ResourcePool])
    for rp in resource_pools.items():
        if not rp[0]:
            continue

        if not hasattr(rp[0], "parent") or not rp[0].parent:
            continue

        # Find resource pool on host
        if self.obj_has_parent(rp[0].parent, host.parent):
            # If no resource_pool selected or it's the selected pool, return it
            if (
                self.module.params["resource_pool"] is None
                or rp[0].name == self.module.params["resource_pool"]
            ):
                return rp[0]

    if self.module.params["resource_pool"] is not None:
        self.module.fail_json(
            msg="Could not find resource_pool %s for selected host %s"
            % (self.module.params["resource_pool"], host.name)
        )
    else:
        self.module.fail_json(msg="Failed to find a resource group for %s" % host.name)