diff --git a/changelogs/fragments/58737-vmware_guest-fix_clone_guest.yml b/changelogs/fragments/58737-vmware_guest-fix_clone_guest.yml new file mode 100644 index 0000000000..f34265c25b --- /dev/null +++ b/changelogs/fragments/58737-vmware_guest-fix_clone_guest.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fixed disk already exists issue while cloning guest in vmware_guest module (https://github.com/ansible/ansible/issues/56861). diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index 6fcb5541bd..bf092837ec 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -1841,7 +1841,9 @@ class PyVmomiHelper(PyVmomi): continue elif vm_obj is None or self.params['template']: # We are creating new VM or from Template - diskspec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create + # Only create virtual device if not backed by vmdk in original template + if diskspec.device.backing.fileName == '': + diskspec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create # which datastore? if expected_disk_spec.get('datastore'): diff --git a/test/integration/targets/vmware_guest/tasks/clone_resize_disks.yml b/test/integration/targets/vmware_guest/tasks/clone_resize_disks.yml new file mode 100644 index 0000000000..a366266c1f --- /dev/null +++ b/test/integration/targets/vmware_guest/tasks/clone_resize_disks.yml @@ -0,0 +1,113 @@ +# Test code for the vmware_guest module. +# Copyright: (c) 2019, Noe Gonzalez +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 5000 + state: started + +- name: kill vcsim + uri: + url: http://{{ vcsim }}:5000/killall +- name: start vcsim with no folders + uri: + url: http://{{ vcsim }}:5000/spawn?datacenter=1&cluster=1&folder=0 + register: vcsim_instance + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 443 + state: started + +- name: get a list of VMS from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=VM + register: vmlist + +- name: get a list of clusters from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=CCR + register: clusters + +- name: get a list of clusters from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=DC + register: datacenters + +- debug: var=vcsim_instance +- debug: var=vmlist + +- name: create new VM + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: clone_resize_disks_original + datacenter: "{{ datacenters['json'][0]|basename }}" + cluster: "{{ clusters['json'][0]|basename }}" + folder: "/{{ (clusters['json'][0]|basename).split('_')[0] }}/vm" + hardware: + num_cpus: 1 + memory_mb: 128 + guest_id: centos7_64Guest + disk: + - size_gb: 1 + type: thin + autoselect_datastore: True + state: poweredoff + +- name: convert to VM template + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: clone_resize_disks_original + datacenter: "{{ datacenters['json'][0]|basename }}" + cluster: "{{ clusters['json'][0]|basename }}" + folder: "/{{ (clusters['json'][0]|basename).split('_')[0] }}/vm" + is_template: True + +- name: clone template and modify disks + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: clone_resize_disks_clone + datacenter: "{{ datacenters['json'][0]|basename }}" + cluster: "{{ clusters['json'][0]|basename }}" + folder: "/{{ (clusters['json'][0]|basename).split('_')[0] }}/vm" + disk: + - size_gb: 2 + type: thin + autoselect_datastore: True + - size_gb: 3 + type: thin + autoselect_datastore: True + template: clone_resize_disks_original + state: poweredoff + register: l_clone_template_modify_disks + +- assert: + that: + - l_clone_template_modify_disks.changed | bool + +- name: delete VM clone & original template + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ item }}" + datacenter: "{{ datacenters['json'][0]|basename }}" + cluster: "{{ clusters['json'][0]|basename }}" + folder: "/{{ (clusters['json'][0]|basename).split('_')[0] }}/vm" + state: absent + with_items: + - clone_resize_disks_original + - clone_resize_disks_clone diff --git a/test/integration/targets/vmware_guest/tasks/main.yml b/test/integration/targets/vmware_guest/tasks/main.yml index e2e54f5963..7c26aaa592 100644 --- a/test/integration/targets/vmware_guest/tasks/main.yml +++ b/test/integration/targets/vmware_guest/tasks/main.yml @@ -28,5 +28,6 @@ - include: linked_clone_d1_c1_f0.yml - include: boot_firmware_d1_c1_f0.yml - include: clone_with_convert.yml +- include: clone_resize_disks.yml #- include: max_connections.yml -- include: mem_reservation.yml \ No newline at end of file +- include: mem_reservation.yml