Repository: ansible-collections/community.vmware

Issue:
vmware_guest_tools_wait  KeyError: 'datacenter'
##### SUMMARY
vmware_guest_tools_wait throwing a KeyError: 'datacenter' because multiple vms with the same name are returned but datacenter is not being passed in from the vmware_guest_tools_wait module.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_guest_tools_wait

##### ANSIBLE VERSION
```
ansible 2.10.10
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/core/tfp-env/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
```

##### CONFIGURATION
```
None
```

##### OS / ENVIRONMENT
```
Red Hat Enterprise Linux release 8.2 (Ootpa)
```

##### STEPS TO REPRODUCE
1. Create multiple vms with the same name in different folders
2. Run community.vmware.vmware_guest_tools_wait module using the vm name with folder like below.


```
- name: Wait for {{ node }}
  community.vmware.vmware_guest_tools_wait:
    hostname: "{{ esxi.ipaddress }}"
    username: "{{ esxi.username }}"
    password: "{{ esxi.password }}"
    name: "{{ node }}"
    folder: "{{ folder }}"
    validate_certs: no
    timeout: 900
  register: vm_facts
```

##### EXPECTED RESULTS
That is doesnt blow up because of keyerror.


##### ACTUAL RESULTS
```
Traceback (most recent call last):
    File "<stdin>", line 102, in <module>
    File "<stdin>", line 94, in _ansiballz_main
    File "<stdin>", line 40, in invoke_module
    File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
        return _run_module_code(code, init_globals, run_name, mod_spec)
    File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
        mod_name, mod_spec, pkg_name, script_name)
    File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest_tools_wait.py", line 205, in <module>
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest_tools_wait.py", line 183, in main
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware.py", line 1078, in get_vm
    KeyError: 'datacenter'
module_stdout: ''
msg: |-
    MODULE FAILURE
    See stdout/stderr for the exact error
```

vmware_guest_tools_wait  KeyError: 'datacenter'
##### SUMMARY
vmware_guest_tools_wait throwing a KeyError: 'datacenter' because multiple vms with the same name are returned but datacenter is not being passed in from the vmware_guest_tools_wait module.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_guest_tools_wait

##### ANSIBLE VERSION
```
ansible 2.10.10
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/core/tfp-env/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
```

##### CONFIGURATION
```
None
```

##### OS / ENVIRONMENT
```
Red Hat Enterprise Linux release 8.2 (Ootpa)
```

##### STEPS TO REPRODUCE
1. Create multiple vms with the same name in different folders
2. Run community.vmware.vmware_guest_tools_wait module using the vm name with folder like below.


```
- name: Wait for {{ node }}
  community.vmware.vmware_guest_tools_wait:
    hostname: "{{ esxi.ipaddress }}"
    username: "{{ esxi.username }}"
    password: "{{ esxi.password }}"
    name: "{{ node }}"
    folder: "{{ folder }}"
    validate_certs: no
    timeout: 900
  register: vm_facts
```

##### EXPECTED RESULTS
That is doesnt blow up because of keyerror.


##### ACTUAL RESULTS
```
Traceback (most recent call last):
    File "<stdin>", line 102, in <module>
    File "<stdin>", line 94, in _ansiballz_main
    File "<stdin>", line 40, in invoke_module
    File "/usr/lib64/python3.6/runpy.py", line 205, in run_module
        return _run_module_code(code, init_globals, run_name, mod_spec)
    File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code
        mod_name, mod_spec, pkg_name, script_name)
    File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
        exec(code, run_globals)
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest_tools_wait.py", line 205, in <module>
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_guest_tools_wait.py", line 183, in main
    File "/tmp/ansible_community.vmware.vmware_guest_tools_wait_payload_sovyp9io/ansible_community.vmware.vmware_guest_tools_wait_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware.py", line 1078, in get_vm
    KeyError: 'datacenter'
module_stdout: ''
msg: |-
    MODULE FAILURE
    See stdout/stderr for the exact error
```



Discussion:
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Files identified in the description:
* [`plugins/modules/vmware_guest_tools_wait.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/vmware_guest_tools_wait.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @Tomorrow9 @goneri @lparkes @pdellaert @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Files identified in the description:
* [`plugins/modules/vmware_guest_tools_wait.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/vmware_guest_tools_wait.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @Tomorrow9 @goneri @lparkes @pdellaert @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/vmware_guest_tools_wait.py b/plugins/modules/vmware_guest_tools_wait.py
--- a/plugins/modules/vmware_guest_tools_wait.py
+++ b/plugins/modules/vmware_guest_tools_wait.py
@@ -69,6 +69,12 @@
      - Max duration of the waiting period (seconds).
      default: 500
      type: int
+   datacenter:
+     description:
+     - Name of the datacenter.
+     - The datacenter to search for a virtual machine.
+     type: str
+     version_added: '1.15.0'
 extends_documentation_fragment:
 - community.vmware.vmware.documentation
 
@@ -112,6 +118,7 @@
     password: "{{ vcenter_password }}"
     name: test-vm
     folder: "/{{datacenter}}/vm"
+    datacenter: "{{ datacenter }}"
   delegate_to: localhost
   register: facts
 '''
@@ -165,6 +172,7 @@ def main():
         moid=dict(type='str'),
         use_instance_uuid=dict(type='bool', default=False),
         timeout=dict(type='int', default=500),
+        datacenter=dict(type='str'),
     )
     module = AnsibleModule(
         argument_spec=argument_spec,


Test patch: