vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 922 forks source link

How to assign LAG uplink to host vmnic while adding hosts to DVS #457

Open rayaraddi opened 6 years ago

rayaraddi commented 6 years ago

How to assign LAG uplink to host vmnic while adding hosts to DVS

rdjagadeesh commented 6 years ago

https://docs.vmware.com/en/VMware-vSphere/6.5/com.vmware.vsphere.networking.doc/GUID-C627D418-C0F0-4D55-9F7D-8F54764A989C.html

udautkarsh commented 3 years ago

Is there any possibility to achieve this using pyvmomi or ansible modules programatically?

udautkarsh commented 3 years ago

I have implemented like this:

pre-requisites: hosts should be added already to dvs. this code will just remove uplinks from default and put into lag ports.

data={'hostname': 'vcenter.example.com, 'username': 'Administrator@vsphere.local, 'password': 'Password'}

si = getVcConnection(vc_fqdn=data['hostname'],vc_user=data['username'],vc_password=data['password'])

content = si.RetrieveContent() host = _getObj(content, [vim.HostSystem], "esxi-1.example.com") dvs_obj = getDVSwitch(si, 'dvswitch1')

uplink = 'vmnic5' dvs_host_configs = list() uplink_port_names = ['Uplink 0', 'Uplink 1', 'Uplink 2', 'Uplink 3'] dvs_config_spec = vim.DistributedVirtualSwitch.ConfigSpec() dvs_config_spec.uplinkPortPolicy = vim.DistributedVirtualSwitch.NameArrayUplinkPortPolicy() dvs_config_spec.uplinkPortPolicy.uplinkPortName = uplink_port_names dvs_config_spec.maxPorts = 60000 dvs_host_config = vim.dvs.HostMember.ConfigSpec()

dvs_host_config.operation = vim.ConfigSpecOperation.add

dvs_host_config.operation = vim.ConfigSpecOperation.edit dvs_host_config.host = host

pnic_specs = list() pnic_spec = vim.dvs.HostMember.PnicSpec() pnic_spec.pnicDevice = uplink pnic_spec.uplinkPortKey = '5' ===========> my dvswitch has 4 uplinks, so the keys were like 0-3. All these uplinks were added to a lag of another 4 ports (4,5,6,7) pnic_specs.append(pnic_spec) dvs_host_config.backing = vim.dvs.HostMember.PnicBacking() dvs_host_config.backing.pnicSpec = pnic_specs dvs_host_configs.append(dvs_host_config) dvs_config_spec.host = dvs_host_configs

dvs_host_spec = dvs_config_spec dvs_host_spec.configVersion = dvs_obj.config.configVersion task = dvs_obj.ReconfigureDvs_Task(dvs_host_spec)

mariolenz commented 3 years ago

I would like to know this, too. I've been trying to understand the API for hours but can't find out how the uplink portgroup and LAGs are connected to each other. A DistributedVirtualSwitchHostMemberPnicSpec doesn't have a lacpGroup property or something. And I don't want to guess the correct uplinkPortKey, especially not when there are several LAGs.

Neither does DVPortgroupConfigInfo have any information about related LAGs, nor does VMwareDvsLacpGroupConfig tell me what (uplink) portgroup it's defined on.

It's a complete mystery to me how to do this. Any help would be highly appreciated!

mariolenz commented 3 years ago

At the moment, I don't think this is possible with pyVmomi. I've studied the vCenter SOAP API for some time now and don't think it can be done. I'd be really interested how this is implemented when you do it via the vCenter UI...

But I see a way to do this in two steps:

  1. Add the host to the DVS
  2. Assign vmnics to LAG uplink ports

I'm working on finding the correct uplinkPortKeys for the LAG uplink ports and think I've found a way to do this. I'll let you know how I did it if it really works.

mariolenz commented 3 years ago

@udautkarsh Not very pretty but I think you can find the correct uplinkPortKeys like this:

# Find LAG "lag1"
for lag in dv_switch.config.lacpGroupConfig:
    if lag.name == "lag1":
        break

# Find all uplink ports on the portgroup for host
portCriteria = vim.dvs.PortCriteria()
portCriteria.host = [host]
portCriteria.portgroupKey = uplink_portgroup.key
portCriteria.uplinkPort = True
ports = dv_switch.FetchDVPorts(portCriteria)

lag_uplinks = []

count = 0
while count < len(lag.uplinkName):
    for port in ports:
        if port.config.name == lag.uplinkName[count]:
            lag_uplinks.append(port.key)
    count += 1

You need to add the host to the switch first.

udautkarsh commented 3 years ago

@mariolenz yeah I assumed hosts are added to dvs. I did not find any way to assign server nics to lag uplinks while adding host itself. Thanks I will improve my code.

mariolenz commented 3 years ago

I did not find any way to assign server nics to lag uplinks while adding host itself.

Neither did I. I'm quite sure it's just not possible. Maybe through the REST API but not through the SOAP API pyVmomi uses.

Thanks I will improve my code.

If you find find a better way, please let me know! Somehow, my code feels a bit wrong to me (if you know what I mean) but at least it seems to work :-)

I think I resent the count a bit, but wanted to make it work for ansible-collections/community.vmware#903 even if it's a bit ugly. Maybe something like for uplinkName in lag.uplinkName would be more pythonic.