vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.01k stars 922 forks source link

Setting computer name (host name) dynamically when cloning a VM #748

Open gilmatok opened 4 months ago

gilmatok commented 4 months ago

Is your feature request related to a problem? Please describe.

I cannot set the computer name (host name) when cloning a VM from a template, so the cloned VM always get the same computer name, which can cause issues with certain programs.

Describe the solution you'd like

There should be a way to set the computer name when cloning a VM.

Describe alternatives you've considered

I've looked at the VMWare documentation for CloneSpec, but I couldn't find anything related to that. The only thing I found was CustomizationSpec, but this refers to the network hostname, not the computer (OS) name

Additional context

No response

prziborowski commented 4 months ago

I believe CustomizationSpec is what you are looking for. In the identity parameter, if you pass in type vim.vm.customization.Sysprep, then the userData of type vim.vm.customization.UserData includes the computerName (of type vim.vm.customization.NameGenerator that has a few different subclass types).

gilmatok commented 3 months ago

@prziborowski thanks. I modified my cloning code to utilize these parameters however, the hostname still remains the same.

Here's my example:

       relospec = vim.vm.RelocateSpec()
        relospec.datastore = datastore
        relospec.pool = cluster.resourcePool
        if linked:
            relospec.diskMoveType = 'createNewChildDiskBacking'

        clonespec = vim.vm.CloneSpec()
        clonespec.location = relospec
        clonespec.powerOn = True

        cust_spec = vim.vm.customization.Specification()

        # Sysprep settings
        cust_spec.identity = vim.vm.customization.Sysprep()
        cust_spec.identity.guiUnattended = vim.vm.customization.GuiUnattended(
            autoLogon=True,
            autoLogonCount=1,
            password=vim.vm.customization.Password(value='Password1', plainText=True)
        )
        cust_spec.identity.userData = vim.vm.customization.UserData(
            fullName='gilmatok',
            orgName='Your Organization',
            computerName=vim.vm.customization.FixedName(name='gilmatok'),
        )

        cust_spec.identity.identification = vim.vm.customization.Identification(joinWorkgroup='WORKGROUP')

        # Network settings
        cust_spec.globalIPSettings = vim.vm.customization.GlobalIPSettings()    

        clonespec.customization=cust_spec
        if linked:
            clonespec.template = False
            clonespec.snapshot=source.snapshot.rootSnapshotList[0].snapshot

        dest_folder = self.get_folder(content, folder_name)

        task = source.Clone(folder=dest_folder, name=dest_name, spec=clonespec)
        if not self.wait_for_task(task):
            raise Exception(f'{"Link-clone" if linked else "Clone"} task of virtual machine "{dest_name}" encountered an error.')