microadapter
Contributor
Contributor

Distributed virtual portgroup and network name using vcenter API

Jump to solution

Hi, I'm trying to correlate the distributed virtual portgroup number and the associated network name using the vcenter API.  For example, we have VMs that use a network (VLAN) named 'MY_VLAN1' and it's assigned to the primary NIC of the VM.  MY_VLAN1 is a distributed virtual portgroup and it has an assigned number of dvportgroup-160.

Using the API I can view the details of the portgroup:

     curl -X GET --header 'Accept: application/json' --header 'vmware-api-session-id: my_sess_id' 'https://myhost.com/rest/vcenter/network?filter.types=DISTRIBUTED_PORTGROUP&filter.networks=dvportgro...'

which returns:

{
  "value": [
  {
  "name": "MY_VLAN1",
  "type": "DISTRIBUTED_PORTGROUP",
  "network": "dvportgroup-160"
  }
  ]
}

You can see the name is 'MY_VLAN1' and the network is dvportgroup-160.

Now when I look at the VM it tells me it's using a different portgroup number.  For example when I access the VM via the API here's the results:

curl -X GET --header 'Accept: application/json' --header 'vmware-api-session-id: my_sess_id' 'https://myhost.com/rest/vcenter/vm/vm-4379'

"name": "myvm1",
  "nics": [
  {
  "value": {
  "start_connected": true,
  "backing": {
  "connection_cookie": 1239675529,
  "distributed_switch_uuid": "e6 6c 04 20 77 08 8g d9-0c dd e7 ba 2g f2 20 46",
  "distributed_port": "17266",
  "type": "DISTRIBUTED_PORTGROUP",
  "network": "dvportgroup-189408"

You can see it says the network is dvportgroup-189408.  But I know the VM is using the 'MY_VLAN1' network which is dvportgroup-160. 

So why does the VM report a different dvportgroup number? 

If the numbers don't match I'm not sure how to correlate which network name the VM is using. 

Or is there another way to determine the network name a VM is using?  I can't see any API calls that directly tell me what network name a VM is using...only what dvportgroup.  But since the numbers don't match I can't map the dvportgroup number back to a network name. 

Any ideas?  Thanks for your help.

0 Kudos
1 Solution

Accepted Solutions
microadapter
Contributor
Contributor

Hi, if it helps anyone else the issue was how the distributed virtual port groups were created in our environment.  It seems we had to migrate (recreate?) a bunch of VLANs on different hosts and during that migration process a powershell script was used.  This script (for whatever reason) created the portgroups with the weird numbers like dvportgroup-189408. 

When our ESX admin manually creates new virtual distributed portgroups the numbers do indeed line up and using the API works okay.

But since we had hundreds of VLANs with weird numbers...the only solution would be to recreate them again.  Which was not an option. 

So in the end we eventually got a powershell one-liner from ESX support that will give you the VLAN name from any given VM.  I turned it into a script that we run from ansible.  Like so:

#!/usr/bin/pwsh

# get the VLAN name from vSphere via powershell module VMware.PowerCLI

# powershell (linux) must be installed

# VMware.PowerCLI powershell module must be installed

Get-Module -ListAvailable VMware.PowerCLI > $null | Import-Module

Connect-VIServer -Server {{ vcenter_hostname }} -Protocol https -User {{ generic_auth_username }} -Password {{ generic_auth_password }} > $null

(Get-NetworkAdapter -VM {{ lookup_vm }} | Select-Object -Property NetworkName -First 1).NetworkName

where {{ lookup_vm }} is the name of the VM you want to know the name of the VLAN it's attached to.  This returns the first NIC on the VM...so sometimes if a VM has more than one NIC you just have to hope the first NIC listed is the primary interface.  In our case we attach a second NIC to VMs for backup purposes on a private 192.168 VLAN.  So every once in a while ESX thinks this private VLAN is the primary interface...but usually it works okay.

View solution in original post

0 Kudos
3 Replies
microadapter
Contributor
Contributor

Or if anyone knows how to map the

   distributed_switch_uuid": "e6 6c 04 20 77 08 8g d9-0c dd e7 ba 2g f2 20 46",

value from the VM API call back to the name of the dvportgroup-xx number?

The uuid seems to be constant (meaning any VM using the same network always reports the same distributed_switch_uuid).  But I can't see how to get the dvportgroup to tell me it's uuid from the REST API (it only reports the name and network...not uuid).

I was hoping there is an API call that I can query the distributed_switch_uuid and have it return the name.  Thanks again.

0 Kudos
microadapter
Contributor
Contributor

My goal is pretty simple (or so I though!).  I just want a VM to tell me the name of the network it's using.  For example I know a VM is using the network named 'MY_VLAN1'.  Is there a way to get that value from the VM?  It seems to me that the VM will only tell you the dvportgroup-xx number value...and not the actual name (MY_VLAN1). 

Any ideas?  Thanks again.

0 Kudos
microadapter
Contributor
Contributor

Hi, if it helps anyone else the issue was how the distributed virtual port groups were created in our environment.  It seems we had to migrate (recreate?) a bunch of VLANs on different hosts and during that migration process a powershell script was used.  This script (for whatever reason) created the portgroups with the weird numbers like dvportgroup-189408. 

When our ESX admin manually creates new virtual distributed portgroups the numbers do indeed line up and using the API works okay.

But since we had hundreds of VLANs with weird numbers...the only solution would be to recreate them again.  Which was not an option. 

So in the end we eventually got a powershell one-liner from ESX support that will give you the VLAN name from any given VM.  I turned it into a script that we run from ansible.  Like so:

#!/usr/bin/pwsh

# get the VLAN name from vSphere via powershell module VMware.PowerCLI

# powershell (linux) must be installed

# VMware.PowerCLI powershell module must be installed

Get-Module -ListAvailable VMware.PowerCLI > $null | Import-Module

Connect-VIServer -Server {{ vcenter_hostname }} -Protocol https -User {{ generic_auth_username }} -Password {{ generic_auth_password }} > $null

(Get-NetworkAdapter -VM {{ lookup_vm }} | Select-Object -Property NetworkName -First 1).NetworkName

where {{ lookup_vm }} is the name of the VM you want to know the name of the VLAN it's attached to.  This returns the first NIC on the VM...so sometimes if a VM has more than one NIC you just have to hope the first NIC listed is the primary interface.  In our case we attach a second NIC to VMs for backup purposes on a private 192.168 VLAN.  So every once in a while ESX thinks this private VLAN is the primary interface...but usually it works okay.

0 Kudos