# Buggy code
def query_service_type_for_vmks(self, host_system, service_type):
    """
    Function to return list of VMKernels
    Args:
        host_system: Host system managed object
        service_type: Name of service type

    Returns: List of VMKernel which belongs to that service type

    """
    vmks_list = []
    query = None
    try:
        query = host_system.configManager.virtualNicManager.QueryNetConfig(service_type)
    except vim.fault.HostConfigFault as config_fault:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            " host config fault : %s" % (service_type, to_native(config_fault.msg))
        )
    except vmodl.fault.InvalidArgument as invalid_argument:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            " invalid arguments : %s" % (service_type, to_native(invalid_argument.msg))
        )
    except Exception as e:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            "%s" % (service_type, to_native(e))
        )

    if not query.selectedVnic:
        return vmks_list
    selected_vnics = [vnic for vnic in query.selectedVnic]
    vnics_with_service_type = [
        vnic.device for vnic in query.candidateVnic if vnic.key in selected_vnics
    ]
    return vnics_with_service_type


# Traceback (nAttributeError)
{
"exception": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 201, in <module>\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 196, in main\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 109, in __init__\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 119, in get_all_vmks_by_service_type\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 146, in query_service_type_for_vmks\nAttributeError: 'NoneType' object has no attribute 'selectedVnic'\n",
"_ansible_no_log": false,
"_ansible_delegated_vars": {
"ansible_host": "localhost"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1569306301.15-75561088679807/AnsiballZ_vmware_vmkernel_facts.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 201, in <module>\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 196, in main\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 109, in __init__\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 119, in get_all_vmks_by_service_type\n  File \"/tmp/ansible_vmware_vmkernel_facts_payload_XR_16X/__main__.py\", line 146, in query_service_type_for_vmks\nAttributeError: 'NoneType' object has no attribute 'selectedVnic'\n",
"changed": false,
"module_stdout": "",
"rc": 1,
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error"
}

# Fixed code
def query_service_type_for_vmks(self, host_system, service_type):
    """
    Function to return list of VMKernels
    Args:
        host_system: Host system managed object
        service_type: Name of service type

    Returns: List of VMKernel which belongs to that service type

    """
    vmks_list = []
    query = None
    try:
        query = host_system.configManager.virtualNicManager.QueryNetConfig(service_type)
    except vim.fault.HostConfigFault as config_fault:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            " host config fault : %s" % (service_type, to_native(config_fault.msg))
        )
    except vmodl.fault.InvalidArgument as invalid_argument:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            " invalid arguments : %s" % (service_type, to_native(invalid_argument.msg))
        )
    except Exception as e:
        self.module.fail_json(
            msg="Failed to get all VMKs for service type %s due to"
            "%s" % (service_type, to_native(e))
        )

    if not query or not query.selectedVnic:
        return vmks_list
    selected_vnics = [vnic for vnic in query.selectedVnic]
    vnics_with_service_type = [
        vnic.device for vnic in query.candidateVnic if vnic.key in selected_vnics
    ]
    return vnics_with_service_type