为方便起见,我最近使用pyvmomi编写了一组有关VMware的工具。 我想克隆模板,然后自定义克隆虚拟机的IP。 但是,发现无法启用startconnected。 这是此模块的摘要代码:
# define adapter
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 = str(setip)
guest_map.adapter.subnetMask = "255.255.255.0"
guest_map.adapter.gateway = "10.10.10.254"
adaptermaps.append(guest_map)
# EOF define adapter
ident = vim.vm.customization.LinuxPrep()
ident.hostName = vim.vm.customization.FixedName()
ident.hostName.name = "testname"
globalip = vim.vm.customization.GlobalIPSettings()
relocate_spec = vim.vm.RelocateSpec()
relocate_spec.datastore = ondatastore
relocate_spec.host = onhost
relocate_spec.pool = oncluster.resourcePool
vmconf_spec = vim.vm.ConfigSpec()
vmconf_spec.cpuHotAddEnabled = True
vmconf_spec.memoryHotAddEnabled = True
devices_change = []
# customization
custom_spec = vim.vm.customization.Specification()
custom_spec.nicSettingMap = adaptermaps
custom_spec.identity = ident
custom_spec.globalIPSettings = globalip
# EOF customization
nic_spec = vim.vm.device.VirtualDeviceSpec()
nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
for dev in target.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
break
else:
raise Exception()
nic_spec.device = dev
nic_spec.device.wakeOnLanEnabled = True
nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic_spec.device.connectable.allowGuestControl = True
# ========== This!!! ==========
nic_spec.device.connectable.startConnected = True
nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
target_network = get_network_by_name(service_instance, "10.10.10.x")
nic_spec.device.backing.network = target_network
nic_spec.device.backing.deviceName = target_network.name
devices_change.append(nic_spec)
vmconf_spec.deviceChange = devices_change
clone_spec = vim.vm.CloneSpec()
clone_spec.powerOn = False
clone_spec.location = relocate_spec
clone_spec.config = vmconf_spec
clone_spec.customization = custom_spec
task = target.Clone(folder=onfolder, name=setname, spec=clone_spec)
在测试之后,可以将取消连接的启动连接状态设置为启用。如下所示。
# define adapter
# 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 = str(setip)
# guest_map.adapter.subnetMask = "255.255.255.0"
# guest_map.adapter.gateway = "10.10.10.254"
# adaptermaps.append(guest_map)
# EOF define adapter
# ident = vim.vm.customization.LinuxPrep()
# ident.hostName = vim.vm.customization.FixedName()
# ident.hostName.name = "testname"
# globalip = vim.vm.customization.GlobalIPSettings()
relocate_spec = vim.vm.RelocateSpec()
relocate_spec.datastore = ondatastore
relocate_spec.host = onhost
relocate_spec.pool = oncluster.resourcePool
vmconf_spec = vim.vm.ConfigSpec()
vmconf_spec.cpuHotAddEnabled = True
vmconf_spec.memoryHotAddEnabled = True
devices_change = []
# customization
# custom_spec = vim.vm.customization.Specification()
# custom_spec.nicSettingMap = adaptermaps
# custom_spec.identity = ident
# custom_spec.globalIPSettings = globalip
# EOF customization
nic_spec = vim.vm.device.VirtualDeviceSpec()
nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
for dev in target.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
break
else:
raise Exception()
nic_spec.device = dev
nic_spec.device.wakeOnLanEnabled = True
nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic_spec.device.connectable.allowGuestControl = True
# ========== This!!! ==========
nic_spec.device.connectable.startConnected = True
nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
target_network = get_network_by_name(service_instance, "10.10.10.x")
nic_spec.device.backing.network = target_network
nic_spec.device.backing.deviceName = target_network.name
devices_change.append(nic_spec)
vmconf_spec.deviceChange = devices_change
clone_spec = vim.vm.CloneSpec()
clone_spec.powerOn = False
clone_spec.location = relocate_spec
clone_spec.config = vmconf_spec
# clone_spec.customization = custom_spec
task = target.Clone(folder=onfolder, name=setname, spec=clone_spec)
那么,如何在克隆过程中启用startconnect而不是执行其他任务?