Cloning VMs via ESXi
VMWare's govc
has support for VM cloning via the vm.clone
command - with one cavaet:
this can only be done through the vCenter API, not directly through ESXi.
When govc vm.clone
is attempted against an ESXi host, ESXi throws the following error:
[27-01-22 02:39:52] Cloning /ha-datacenter/vm/original-vm to vm-clone...Error: The operation is not supported on the object.govc: The operation is not supported on the object.
It's still possible to simply copy the directory and register it, but we must patch the VMX file,
otherwise the VM will be unable to start automatically, and whoever is logged into the ESXi will see a banner stating This virtual machine might have been moved or copied
.
vmware_params: &vmware_paramsGOVC_USERNAME: usernameGOVC_PASSWORD: passwordGOVC_URL: https://myesxiGOVC_DATASTORE: "Datastore Name"GOVC_INSECURE: 1jobs:- name: clone-vm-with-esxiplan:- task: clone-vmconfig:platform: linuximage_resource:type: registry-imagesource: { repository: oozie/vmware-aws }params:SOURCE_VM: stemcell-virtual-machine.ovaDESTINATION_VM: my-cloned-vm2.ova<<: *vmware_paramsrun:path: bashargs:- -c- |set -xgovc datastore.cp "${SOURCE_VM}" "${DESTINATION_VM}"# patch the VMX filegovc datastore.download "${DESTINATION_VM}/${SOURCE_VM}.vmx" "${SOURCE_VM}.vmx"echo 'answer.msg.uuid.altered="I moved it"' >> "${SOURCE_VM}.vmx"govc datastore.upload "${SOURCE_VM}.vmx" "${DESTINATION_VM}/${SOURCE_VM}.vmx"govc vm.register -name "${DESTINATION_VM}" "${DESTINATION_VM}/${SOURCE_VM}.vmx"
The output from the resulting task will look like this:
+ govc datastore.cp stemcell-virtual-machine.ova my-cloned-vm2.ova[27-01-22 03:02:44] Copying [Datastore Name] stemcell-virtual-machine.ova to [Datastore Name] my-cloned-vm2.ova...OK+ govc datastore.download my-cloned-vm2.ova/stemcell-virtual-machine.ova.vmx stemcell-virtual-machine.ova.vmx[27-01-22 03:02:44] Downloading... OK+ echo 'answer.msg.uuid.altered="I moved it"'+ govc datastore.upload stemcell-virtual-machine.ova.vmx my-cloned-vm2.ova/stemcell-virtual-machine.ova.vmx[27-01-22 03:02:44] Uploading... OK+ govc vm.register -name my-cloned-vm2.ova my-cloned-vm2.ova/stemcell-virtual-machine.ova.vmx
and a cloned VM will be listed in ESXi's user interface.