vsphere_distributed_port_group

The vsphere_distributed_port_group resource can be used to manage vSphere distributed virtual port groups. These port groups are connected to distributed virtual switches, which can be managed by the vsphere_distributed_virtual_switch resource.

Distributed port groups can be used as networks for virtual machines, allowing VMs to use the networking supplied by a distributed virtual switch (DVS), with a set of policies that apply to that individual newtork, if desired.

For an overview on vSphere networking concepts, see this page. For more information on vSphere DVS portgroups, see this page.

Example Usage

The configuration below builds on the example given in the vsphere_distributed_virtual_switch resource by adding the vsphere_distributed_port_group resource, attaching itself to the DVS created here and assigning VLAN ID 1000.

variable "esxi_hosts" {
  default = [
    "esxi1",
    "esxi2",
    "esxi3",
  ]
}

variable "network_interfaces" {
  default = [
    "vmnic0",
    "vmnic1",
    "vmnic2",
    "vmnic3",
  ]
}

data "vsphere_datacenter" "dc" {
  name = "dc1"
}

data "vsphere_host" "host" {
  count         = "${length(var.esxi_hosts)}"
  name          = "${var.esxi_hosts[count.index]}"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

resource "vsphere_distributed_virtual_switch" "dvs" {
  name          = "terraform-test-dvs"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"

  uplinks         = ["uplink1", "uplink2", "uplink3", "uplink4"]
  active_uplinks  = ["uplink1", "uplink2"]
  standby_uplinks = ["uplink3", "uplink4"]

  host {
    host_system_id = "${data.vsphere_host.host.0.id}"
    devices        = ["${var.network_interfaces}"]
  }

  host {
    host_system_id = "${data.vsphere_host.host.1.id}"
    devices        = ["${var.network_interfaces}"]
  }

  host {
    host_system_id = "${data.vsphere_host.host.2.id}"
    devices        = ["${var.network_interfaces}"]
  }
}

resource "vsphere_distributed_port_group" "pg" {
  name                            = "terraform-test-pg"
  distributed_virtual_switch_uuid = "${vsphere_distributed_virtual_switch.dvs.id}"

  vlan_id = 1000
}

Overriding DVS policies

All of the default port policies available in the vsphere_distributed_virtual_switch resource can be overridden on the port group level by specifying new settings for them.

As an example, we also take this example from the vsphere_distributed_virtual_switch resource where we manually specify our uplink count and uplink order. While the DVS has a default policy of using the first uplink as an active uplink and the second one as a standby, the overridden port group policy means that both uplinks will be used as active uplinks in this specific port group.

resource "vsphere_distributed_virtual_switch" "dvs" {
  name          = "terraform-test-dvs"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"

  uplinks         = ["tfup1", "tfup2"]
  active_uplinks  = ["tfup1"]
  standby_uplinks = ["tfup2"]
}

resource "vsphere_distributed_port_group" "pg" {
  name                            = "terraform-test-pg"
  distributed_virtual_switch_uuid = "${vsphere_distributed_virtual_switch.dvs.id}"

  vlan_id = 1000

  active_uplinks  = ["tfup1", "tfup2"]
  standby_uplinks = []
}

Argument Reference

The following arguments are supported:

  • name - (Required) The name of the port group.
  • distributed_virtual_switch_uuid - (Required) The ID of the DVS to add the port group to. Forces a new resource if changed.
  • type - (Optional) The port group type. Can be one of earlyBinding (static binding) or ephemeral. Default: earlyBinding.
  • description - (Optional) An optional description for the port group.
  • number_of_ports - (Optional) The number of ports available on this port group. Cannot be decreased below the amount of used ports on the port group.
  • auto_expand - (Optional) Allows the port group to create additional ports past the limit specified in number_of_ports if necessary. Default: true.
  • port_name_format - (Optional) An optional formatting policy for naming of the ports in this port group. See the portNameFormat attribute listed here for details on the format syntax.

  • network_resource_pool_key - (Optional) The key of a network resource pool to associate with this port group. The default is -1, which implies no association.

  • custom_attributes (Optional) Map of custom attribute ids to attribute value string to set for port group. See here for a reference on how to set values for custom attributes.

Policy options

In addition to the above options, you can configure any policy option that is available under the vsphere_distributed_virtual_switch policy options section. Any policy option that is not set is inherited from the DVS, its options propagating to the port group.

See the link for a full list of options that can be set.

Port override options

The following options below control whether or not the policies set in the port group can be overridden on the individual port:

Attribute Reference

The following attributes are exported:

  • config_version: The current version of the port group configuration, incremented by subsequent updates to the port group.

Importing

An existing port group can be imported into this resource via the path to the port group, via the following command:

terraform import vsphere_distributed_port_group.pg /dc1/network/pg

The above would import the port group named pg that is located in the dc1 datacenter.

© 2018 HashiCorp
Licensed under the MPL 2.0 License.
https://www.terraform.io/docs/providers/vsphere/r/distributed_port_group.html