python pyvmomi operation VMware (eight): according to template clone virtual machine (type is Windows) configuration password

1. Requirements:
1. The cloned virtual machine is of Windows type
2. The cloned virtual machine must be able to specify IP, host name and other information (the previous section has been implemented)
2. Document research:
Insert picture description here
Insert picture description here
Now there is a very difficult problem, the computer New administrator password. To specify that the password should be set to blank (that is, there is no password), set the password value to NULL. Due to encryption, "" is not a valid value. If the XML file is generated by the VirtualCenter Customization Wizard, the password will be encrypted. Otherwise, the client should set the plaintext attribute to true so that the customization process will not attempt to decrypt the string.
Insert picture description here
Obviously, it can be seen that the value field is a string type, so it is the setting field of the last password.
3. Idea: The
first step: the type of guiUnattended is CustomizationGuiUnattended, find the object generation method of CustomizationGuiUnattended; the
second step: the type of password is CustomizationPassword, find the object generation method of CustomizationPassword;
Fourth, realize the object generation method:

# CustomizationGuiUnattended的对象产生方式
ident.guiUnattended = vim.vm.customization.GuiUnattended()
# CustomizationPassword的对象产生方式
password = vim.vm.customization.Password()

Five, test code:

    def get_customspec(self, template, vm_ip=None, vm_subnetmask=None, vm_gateway=None, vm_dns=None,
                       vm_domain=None, vm_hostname=None, vm_password=None):
        # guest NIC settings
        adaptermaps = []
        guest_map = vim.vm.customization.AdapterMapping()
        guest_map.adapter = vim.vm.customization.IPSettings()
        guest_map.adapter.ip = vim.vm.customization.FixedIp()
        guest_map.adapter.ip.ipAddress = vm_ip
        guest_map.adapter.subnetMask = vm_subnetmask
        guest_map.adapter.gateway = vm_gateway
        if vm_domain:
            guest_map.adapter.dnsDomain = vm_domain
        adaptermaps.append(guest_map)

        # DNS settings
        globalip = vim.vm.customization.GlobalIPSettings()
        if vm_dns:
            if vm_dns.find(',') > -1:
                globalip.dnsServerList = vm_dns.split(',')
            else:
                globalip.dnsServerList = [vm_dns]
            globalip.dnsSuffixList = vm_domain

        # Hostname settings

        # 如果模板是windows
        if 'windows' in template.config.guestId:
            ident = vim.vm.customization.Sysprep()
            ident.userData = vim.vm.customization.UserData()
            ident.userData.computerName = vim.vm.customization.FixedName()
            if vm_hostname:
                ident.userData.computerName.name = vm_hostname
                ident.userData.fullName = 'Administrator'
                ident.userData.orgName = 'Administrators'

            ident.guiUnattended = vim.vm.customization.GuiUnattended()
            ident.identification = vim.vm.customization.Identification()
            password = vim.vm.customization.Password()
            password.value = vm_password if vm_password else 'root'
            ident.guiUnattended.password = password
            if vm_domain:
                ident.identification.domainAdmin = vm_domain

        else:
            # 如果模板不是windows
            ident = vim.vm.customization.LinuxPrep()
            if vm_domain:
                ident.domain = vm_domain
            ident.hostName = vim.vm.customization.FixedName()
            if vm_hostname:
                ident.hostName.name = vm_hostname

        customspec = vim.vm.customization.Specification()
        customspec.nicSettingMap = adaptermaps
        customspec.globalIPSettings = globalip
        customspec.identity = ident
        return customspec

An error
Insert picture description here
occurred during cloning: it explained a problem: the direction of thinking of setting the password is correct.
Solution:
1. The required password string (encrypted) must be in accordance with the decryption method
. 2. Set the Windows virtual machine not to decrypt, and directly set the original value of the string password (unencrypted).
The second is more in line with ours Idea:
The password string. It is encrypted if the associated plainText flag is false.
After translation: password string. If the associated plaintext flag is false, it will be encrypted.
The meaning is: We mark the associated plaintext as true, so that encryption will not be performed, so that the Windows virtual machine will not decrypt it.

    def get_customspec(self, template, vm_ip=None, vm_subnetmask=None, vm_gateway=None, vm_dns=None,
                       vm_domain=None, vm_hostname=None, vm_password=None):
        # guest NIC settings
        adaptermaps = []
        guest_map = vim.vm.customization.AdapterMapping()
        guest_map.adapter = vim.vm.customization.IPSettings()
        guest_map.adapter.ip = vim.vm.customization.FixedIp()
        guest_map.adapter.ip.ipAddress = vm_ip
        guest_map.adapter.subnetMask = vm_subnetmask
        guest_map.adapter.gateway = vm_gateway
        if vm_domain:
            guest_map.adapter.dnsDomain = vm_domain
        adaptermaps.append(guest_map)

        # DNS settings
        globalip = vim.vm.customization.GlobalIPSettings()
        if vm_dns:
            if vm_dns.find(',') > -1:
                globalip.dnsServerList = vm_dns.split(',')
            else:
                globalip.dnsServerList = [vm_dns]
            globalip.dnsSuffixList = vm_domain

        # Hostname settings

        # 如果模板是windows
        if 'windows' in template.config.guestId:
            ident = vim.vm.customization.Sysprep()
            ident.userData = vim.vm.customization.UserData()
            ident.userData.computerName = vim.vm.customization.FixedName()
            if vm_hostname:
                ident.userData.computerName.name = vm_hostname
                ident.userData.fullName = 'Administrator'
                ident.userData.orgName = 'Administrators'

            ident.guiUnattended = vim.vm.customization.GuiUnattended()
            ident.identification = vim.vm.customization.Identification()
            password = vim.vm.customization.Password()
            password.value = vm_password if vm_password else 'root'
            # 设置密码不经过加密验证
            password.plainText = True
            ident.guiUnattended.password = password
            if vm_domain:
                ident.identification.domainAdmin = vm_domain

        else:
            # 如果模板不是windows
            ident = vim.vm.customization.LinuxPrep()
            if vm_domain:
                ident.domain = vm_domain
            ident.hostName = vim.vm.customization.FixedName()
            if vm_hostname:
                ident.hostName.name = vm_hostname

        customspec = vim.vm.customization.Specification()
        customspec.nicSettingMap = adaptermaps
        customspec.globalIPSettings = globalip
        customspec.identity = ident
        return customspec

Result: The clone is successful, and the cloned Windows virtual machine needs a password to log in.

Guess you like

Origin blog.csdn.net/qq_42631707/article/details/108658981