Question

I've scoured the docs (http://pubs.vmware.com/vsphere-55/index.jsp) and can't find anything about adding virtual machines to vlans. There's plenty of info on creating and configuring them, but not how to add virtual machines to them through pyvmomi (or esxcli if necessary). I'm pretty sure it can be done, as you can do it easily through the vSphere client, but I'm looking to do it in an automated fashion.

Was it helpful?

Solution

The code from https://github.com/rreubenur/vmware-python-examples/blob/master/clone_vm_on_each_host/reconfigure_vnics.py ended up helping me a great deal:

nicspec = vim.vm.device.VirtualDeviceSpec()
nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
nicspec.device = nic_type
nicspec.device.wakeOnLanEnabled = True
nicspec.device.deviceInfo = vim.Description()
nicspec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
nicspec.device.backing.network = self.get_obj(content, [vim.Network], net_name)
nicspec.device.backing.deviceName = net_name

nicspec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nicspec.device.connectable.startConnected = True
nicspec.device.connectable.allowGuestControl = True

devices.append(nicspec)

vmconf = vim.vm.ConfigSpec(deviceChange=devices)

Allowed me to create the proper config object for connecting to a virtual network on a standard switch.

nic_type is the type of network device (e1000, pc32, etc.), net_name is the name of the network (portgroup). The rest is pretty self-explanatory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top