# Buggy code
def exec_module(self, **kwargs):
    for key in self.module_arg_spec:
        setattr(self, key, kwargs[key])

    if self.name and not self.resource_group:
        self.fail("Parameter error: resource group required when filtering by name.")
    if self.name:
        self.results["vms"] = self.get_item()
    else:
        self.results["vms"] = self.list_items()

    return self.results


# Traceback (msrest.exceptions.ValidationError)
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "No handlers could be found for logger \"msrestazure.azure_active_directory\"\nTraceback (most recent call last):\n
File \"/tmp/ansible-tmp-1552941958.2-220411846899861/AnsiballZ_azure_rm_virtualmachine_facts.py\", line 113, in <module>\n    _ansiballz_main()\n
File \"/tmp/ansible-tmp-1552941958.2-220411846899861/AnsiballZ_azure_rm_virtualmachine_facts.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n
File \"/tmp/ansible-tmp-1552941958.2-220411846899861/AnsiballZ_azure_rm_virtualmachine_facts.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n
File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/__main__.py\", line 347, in <module>\n    \n  File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/__main__.py\", line 343, in main\n    \n
File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/__main__.py\", line 224, in __init__\n    \n
File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/ansible_azure_rm_virtualmachine_facts_payload.zip/ansible/module_utils/azure_rm_common.py\", line 308, in __init__\n
File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/__main__.py\", line 236, in exec_module\n    \n
File \"/tmp/ansible_azure_rm_virtualmachine_facts_payload__muqp2/__main__.py\", line 263, in list_items\n    \n
File \"/home/svc_ucoansible/.local/lib/python2.7/site-packages/msrest/paging.py\", line 121, in __next__\n    self.advance_page()\n
File \"/home/svc_ucoansible/.local/lib/python2.7/site-packages/msrest/paging.py\", line 107, in advance_page\n    self._response = self._get_next(self.next_link)\n
File \"/home/svc_ucoansible/.local/lib/python2.7/site-packages/azure/mgmt/compute/compute/v2017_03_30/operations/virtual_machines_operations.py\", line 664, in internal_paging\n    'resourceGroupName': self._serialize.url(\"resource_group_name\", resource_group_name, 'str'),\n
File \"/home/svc_ucoansible/.local/lib/python2.7/site-packages/msrest/serialization.py\", line 488, in url\n    data = self.validate(data, name, required=True, **kwargs)\n
File \"/home/svc_ucoansible/.local/lib/python2.7/site-packages/msrest/serialization.py\", line 558, in validate\n    raise ValidationError(\"required\", name, True)\n
msrest.exceptions.ValidationError: Parameter 'resource_group_name' can not be None.\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1}
to retry, use: --limit @/opt/automation/Ansible/find-config.retry

# Fixed code
def exec_module(self, **kwargs):
    for key in self.module_arg_spec:
        setattr(self, key, kwargs[key])

    if self.name and not self.resource_group:
        self.fail("Parameter error: resource group required when filtering by name.")
    if self.name:
        self.results["vms"] = self.get_item()
    elif self.resource_group:
        self.results["vms"] = self.list_items_by_resourcegroup()
    else:
        self.results["vms"] = self.list_all_items()

    return self.results