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.
Asked
Active
Viewed 5,597 times
0
-
The docs recommend changing the portgroup in VirtualEthernetCardDistributedVirtualPortBackingInfo (http://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo.html). Now the problem is propagating it to the server. – Magitrek Jun 04 '14 at 12:52
-
Turns out that's only for distributed virtual portgroups. Standard ones use my answer below. – Magitrek Jun 11 '14 at 21:23
1 Answers
2
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.

Magitrek
- 545
- 5
- 19