Search code examples
vmwarepyvmomi

Deploy VM from template and set VM and DNS name


I’m using pyVmomi to deploy a VM from a template on vSphere, this woks ok, the new VM get the name I sent as parameter but I want that the DNS name \ hostname will be same as VM.
Is there a way to set the hostname when doing the actual clone ?
If not how can I do that after the new VM was created ?

Here is part of the code I'm using:

# RelocateSpec
relospec = vim.vm.RelocateSpec()
relospec.datastore = datastore
relospec.pool = resource_pool

# ConfigSpec
configSpec = vim.vm.ConfigSpec()
configSpec.annotation = "This is the annotation for this VM"

# CloneSpec
clonespec = vim.vm.CloneSpec()
clonespec.location = relospec
clonespec.powerOn = power_on
clonespec.config = configSpec

print ("cloning VM...")
task = template.Clone(folder=destfolder, name=vm_name, spec=clonespec)
wait_for_task(task)

Solution

  • I think you need a clonespec.customization (vim.vm.customization.Specification). You should be able to specify the hostname there somehow or other.

    Oh, as far as I know VMware Tools must be installed for guest OS customization.

    Hope that helps.