diff --git a/cloud/vmware/vsphere_guest.py b/cloud/vmware/vsphere_guest.py index a14f807e049..d98f585ca49 100644 --- a/cloud/vmware/vsphere_guest.py +++ b/cloud/vmware/vsphere_guest.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python2 # -*- coding: utf-8 -*- # This file is part of Ansible @@ -701,12 +701,77 @@ def deploy_template(vsphere_client, guest, resource_pool, template_src, esxi, mo msg="Could not clone selected machine: %s" % e ) +# example from https://github.com/kalazzerx/pysphere/blob/master/examples/pysphere_create_disk_and_add_to_vm.py +# was used. +def update_disks(vsphere_client, vm, module, vm_disk, changes): + request = VI.ReconfigVM_TaskRequestMsg() + changed = False + + for cnf_disk in vm_disk: + disk_id = re.sub("disk", "", cnf_disk) + found = False + for dev_key in vm._devices: + if vm._devices[dev_key]['type'] == 'VirtualDisk': + hdd_id = vm._devices[dev_key]['label'].split()[2] + if disk_id == hdd_id: + found = True + continue + if not found: + it = VI.ReconfigVM_TaskRequestMsg() + _this = request.new__this(vm._mor) + _this.set_attribute_type(vm._mor.get_attribute_type()) + request.set_element__this(_this) + + spec = request.new_spec() + + dc = spec.new_deviceChange() + dc.Operation = "add" + dc.FileOperation = "create" + + hd = VI.ns0.VirtualDisk_Def("hd").pyclass() + hd.Key = -100 + hd.UnitNumber = int(disk_id) + hd.CapacityInKB = int(vm_disk[cnf_disk]['size_gb']) * 1024 * 1024 + hd.ControllerKey = 1000 + + # module.fail_json(msg="peos : %s" % vm_disk[cnf_disk]) + backing = VI.ns0.VirtualDiskFlatVer2BackingInfo_Def("backing").pyclass() + backing.FileName = "[%s]" % vm_disk[cnf_disk]['datastore'] + backing.DiskMode = "persistent" + backing.Split = False + backing.WriteThrough = False + backing.ThinProvisioned = False + backing.EagerlyScrub = False + hd.Backing = backing + + dc.Device = hd + + spec.DeviceChange = [dc] + request.set_element_spec(spec) + + ret = vsphere_client._proxy.ReconfigVM_Task(request)._returnval + + # Wait for the task to finish + task = VITask(ret, vsphere_client) + status = task.wait_for_state([task.STATE_SUCCESS, + task.STATE_ERROR]) + + if status == task.STATE_SUCCESS: + changed = True + changes[cnf_disk] = vm_disk[cnf_disk] + elif status == task.STATE_ERROR: + module.fail_json( + msg="Error reconfiguring vm: %s, [%s]" % ( + task.get_error_message(), + vm_disk[cnf_disk])) + return changed, changes + def reconfigure_vm(vsphere_client, vm, module, esxi, resource_pool, cluster_name, guest, vm_extra_config, vm_hardware, vm_disk, vm_nic, state, force): spec = None changed = False changes = {} - request = VI.ReconfigVM_TaskRequestMsg() + request = None shutdown = False poweron = vm.is_powered_on() @@ -714,6 +779,10 @@ def reconfigure_vm(vsphere_client, vm, module, esxi, resource_pool, cluster_name cpuHotAddEnabled = bool(vm.properties.config.cpuHotAddEnabled) cpuHotRemoveEnabled = bool(vm.properties.config.cpuHotRemoveEnabled) + changed, changes = update_disks(vsphere_client, vm, + module, vm_disk, changes) + request = VI.ReconfigVM_TaskRequestMsg() + # Change Memory if 'memory_mb' in vm_hardware: