How to get vlans available in VMware Vcenter?


How to get vlans available in VMware Vcenter?


Hi Andres

If you just have a single vCenter and want to retrieve all of the vlans then from automate it would just be:

vlans = $evm.vmdb('lan').all

Hope this helps,


@pemcg Thanks Peter. It’s been very helpful.



The pproblem is that my vlan is: vlan80(dvSwitch1). When i list de vlan with vlans = $evm.vmdb(‘lan’).all only take the name and no take the prefij: dvSwitch1

But, when i go to the infraestructure-> virtual machine-> lifecycle-> provision vms . In the folder network, the list is ok.


Set vLan from Service Dialogs

Hi @pemcg. Do you know how to get the ip address of a provisioned VM?
Thanks for your help.


You can use vm.ipaddresses.join


That drop down is just appending the name of the switch. You could get the same effect yourself in a dynamic dialog such as this (this example runs from a button on a cluster object in the WebUI, and lists all lans in the cluster):

  lans = {}
  $evm.root['ems_cluster'].hosts.each do |host|
    lans['!'] = '-- select from list --'
    host.lans.each do |lan|
      lans[] = "#{} (#{})"

  list_values = {
    'sort_by'    => :value,
    'data_type'  => :string,
    'required'   => true,
    'values'     => lans
  list_values.each { |key, value| $evm.object[key] = value }

 rescue => err
  $evm.log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
  exit MIQ_STOP


I have this problem when I aply the scritp:

ERROR – : [undefined method `hosts’ for nil:NilClass]



Are you running the script from a custom button on your cluster object?

It looks like you don’t have $evm.root[‘ems_cluster’] defined.



I am apply this script to a custom service dialog:


ok, so you need to lookup the cluster object yourself, something like

cluster = $evm.vmdb(:ems_cluster).where(:name => 'my_cluster_name').first
cluster.hosts.each do |host|


Sorry, but I do not understand. My cluster is called: Cluster_Internet_LC. How would it stay?


Yeah!!! Is OK!!!


The problem today is:



Hi, the new.

The problem with of has no EMS is ok, the problem is the template, but when i executed the sctrip and send the value, the value of vlan es number, in this case is : 870. This configuration is no read for the vmware, attach of image with the problem: