From f5e9d19af76496dfbfc0b9d7907d84d533230b61 Mon Sep 17 00:00:00 2001
From: Abhijeet Kasurde <akasurde@redhat.com>
Date: Tue, 18 Jul 2017 17:18:21 +0530
Subject: [PATCH] Correct usage of FindByUuid in vmware module util (#26040)

This fix corrects the usage of function FindByUuid by
specifying correct parameter 'uuid' and 'instanceUuid'
as documentation of VMWare's API.

Fixes: #24398, #24835, #25713

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
---
 lib/ansible/module_utils/vmware.py                    |  3 ++-
 .../modules/cloud/vmware/vmware_guest_facts.py        | 11 ++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py
index 97ab4c1db53..f0881c66f3b 100644
--- a/lib/ansible/module_utils/vmware.py
+++ b/lib/ansible/module_utils/vmware.py
@@ -182,7 +182,8 @@ def find_vm_by_id(content, vm_id, vm_id_type="vm_name", datacenter=None, cluster
         if isinstance(vm, vim.VirtualMachine):
             vm = None
     elif vm_id_type == 'uuid':
-        vm = si.FindByUuid(datacenter=datacenter, instanceUuid=vm_id, vmSearch=True)
+        # Search By BIOS UUID rather than instance UUID
+        vm = si.FindByUuid(datacenter=datacenter, instanceUuid=False, uuid=vm_id, vmSearch=True)
     elif vm_id_type == 'ip':
         vm = si.FindByIp(datacenter=datacenter, ip=vm_id, vmSearch=True)
     elif vm_id_type == 'vm_name':
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py
index 601a5713b30..d067e95d7af 100644
--- a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py
@@ -41,7 +41,6 @@ options:
    name:
         description:
             - Name of the VM to work with
-        required: True
    name_match:
         description:
             - If multiple VMs matching the name, use the first or last found
@@ -168,13 +167,14 @@ def main():
                 default=os.environ.get('VMWARE_PASSWORD')
             ),
             validate_certs=dict(required=False, type='bool', default=True),
-            name=dict(required=True, type='str'),
+            name=dict(required=False, type='str'),
             name_match=dict(required=False, type='str', default='first'),
             uuid=dict(required=False, type='str'),
             folder=dict(required=False, type='str', default='/vm'),
             datacenter=dict(required=True, type='str'),
         ),
         required_together=[('name', 'folder')],
+        required_one_of=[['name', 'uuid']],
     )
 
     # FindByInventoryPath() does not require an absolute path
@@ -194,7 +194,12 @@ def main():
         except Exception as exc:
             module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc))
     else:
-        module.fail_json(msg="Unable to gather facts for non-existing VM %(name)s" % module.params)
+        msg = "Unable to gather facts for non-existing VM "
+        if module.params['name']:
+            msg += "%(name)s" % module.params
+        elif module.params['uuid']:
+            msg += "%(uuid)s" % module.params
+        module.fail_json(msg=msg)
 
 if __name__ == '__main__':
     main()