acidgo / pyvmomi Goto Github PK
View Code? Open in Web Editor NEWuse the doc
License: Apache License 2.0
use the doc
License: Apache License 2.0
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 = []
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
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
task = target.Clone(folder=onfolder, name=setname, spec=clone_spec)
For convenience, I recently wrote a set of tools about VMware using pyvmomi.
I want to clone the template and then customize the IP of the cloned virtual machine.
However, it was found that the startconnected could not be enable.
Here is the summary code for this module:
# 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)
After testing, the startconnected status can be set to enable when I cancel the customization.As shown below.
# 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)
So, how can I enable startconnected during the cloning process instead of performing additional tasks?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.