Repository: ansible/ansible

Issue:
vmware_guest ignores UUID on vm creation
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
cloud / vmware_guest

##### ANSIBLE VERSION
```
ansible 2.4.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/fxxxxx/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.14 (default, Nov  2 2017, 18:42:05) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]

```

##### CONFIGURATION
ansible-config dump --only-changed returns nothing (empty). 

##### OS / ENVIRONMENT
Controller OS: Fedora 26 on x86_64
Target: vmware vSphere 6.5 Update 1 (Version 6.5.0.10000 Build 5973321)

Using pyvmomi Version 6.5.0.2017.5.post1

##### SUMMARY
Passing an UUID to a vmware_guest: entry for a non-existent VM will create a new VM with a new UUID, not the one that was passed in. 

##### STEPS TO REPRODUCE

```yaml
- name: ensure  vm
  vmware_guest:
    hostname: vcenter.dxxx.xxxx.com
    datacenter: "{{ vcenter_datacenter }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_password }}"
    validate_certs: no

    name: "{{ vm_instance.name }}"
    folder: CloudX
    template: "CentOS 7 Template"
    state: present
    uuid: "{{ vm_instance.uuid }}"
    hardware:
      memory_mb: 16384
      num_cpus: 4
      scsi: paravirtual
    networks:
    - name: "CloudX"
      vlan: 668
      type: static
      ip: "{{ vm_instance.ip }}"
      netmask: "{{ vm_instance.subnetmask }}"
      gateway: "{{ vm_instance.gateway }}"
    customization:
      dns_servers:
      - 10.22.1.200
      - 10.22.1.205
      dns_suffix:
      - dxxx.xxxx.com
      hostname: "{{ vm_instance.name }}"
      domain: dxxx.xxxx.com

    wait_for_ip_address: no
  delegate_to: localhost
  register: new_vm

- name: print vm uuid
  debug:
    msg: "vm uuid: {{ new_vm.instance.hw_product_uuid }}"
```
The printed out message will contain a uuid that differs from the passed-in uuid on the first run (vm creation). 

Subsequent runs will consistently print the actual UUID that is also visible in vcenter; if the ansible playbook is adapted to use that UUID, everything works. 

This behavior means the following is necessary: 
1. Run playbook
2. Copy actual UUID from playbook output or vcenter and copy-paste it into playbook
3. Re-Run playbook n times

##### EXPECTED RESULTS
The newly created VM uses the passed-in UUID for creating the new VM.

##### ACTUAL RESULTS
The newly created VM uses a random uuid not related to the passed-in UUID.


Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/vmware/vmware_guest.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/vmware/vmware_guest.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 @dagwieers @dav1x @jctanner @nerzhul @pdellaert
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@l3m Thanks for reporting this issue.The UUIDs are generated by VMware for newer machine, so specifying UUID for non-existing VM is irrelevant. Once the VM is created newly created UUID gets in effect, all subsequent operations after creation with new UUID works. 

Let me know if you need any other help. I am (akasurde) on #ansible-vmware @ irc.freenode.net

Resolving patch:
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py
--- a/lib/ansible/modules/cloud/vmware/vmware_guest.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py
@@ -52,6 +52,7 @@
     description:
     - UUID of the instance to manage if known, this is VMware's unique identifier.
     - This is required if name is not supplied.
+    - Please note that a supplied UUID will be ignored on VM creation, as VMware creates the UUID internally.
   template:
     description:
     - Template used to create VM.


Test patch: