Closed snow-with-tea closed 1 week ago
The CloneVm method only works from vCenter. You can't execute it directly from ESXi.
The CloneVm method only works from vCenter. You can't execute it directly from ESXi.
Is there any other way to achieve this effect? Creating one by one is very painful
If your template VM is very simple, then it is possible you could re-implement what clone method is doing (copying files and creating a new VM). I haven't done something like this in many years, so I don't know how well it still works. There is a VirtualDiskManager.CopyVirtualDisks
So from your esxi class, you'd call:
from pyVim.task import WaitForTask
task = self.content.virtualDiskManager.CopyVirtualDisks(sourceName=sourceName, destName=destName)
WaitForTask(task)
And once the disks are copied, you can use this create_vm.py sample: https://github.com/vmware/pyvmomi-community-samples/pull/523/files for an idea on attaching existing disks for a new VM. The other tricky part would be creating a ConfigSpec that looks similar to the ConfigInfo of the source VM, but replacing identifiers so that you have a unique VM. I don't have any pointers to any code that does that.
Alternatively I believe you can export the VM (as an ovf/ova), and import it to get a new VM. This won't be very performant though as it would be transferring disks through your client. For that, you would combine https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/export_vm.py and https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/deploy_ovf.py Or could leverage the ovftool binary that I believe Workstation and Fusion have.
If you are just using the same template over and over for the clones, you could host the ovf/vmdk on a webserver that ESXi has access to (might have to configure firewall rules for that) and the deployOvf/deployOva can be used to pull those files.
The easiest way is to use vCenter though.
Describe the bug
I have a virtual machine named "template_centos7", and now I want to clone it, but I have failed using the following script
Did I make a mistake in writing somewhere
Reproduction steps
Expected behavior
I hope to clone another virtual machine from an existing one
Additional context
No response