Dynamic blocks involving data sources

If I’m understanding correctly what you want to achieve, I think the following should do it:

variable "networks" {
  type = list(object({
    label = string
  }))
}

data "vsphere_network" "network" {
  count = length(var.networks)

  name          = var.networks[count.index].label
  datacenter_id = data.vsphere_datacenter.dc.id
}

resource "vsphere_virtual_machine" "vm" {
  # ...

  dynamic "network_interface" {
    for_each = data.vsphere_network.network
    content {
      networkid = network_interface.value.id
    }
  }
}

The key is to use multiple instances of the data resource, with one instance per network you want to connect with, and then generate a network_interface block for each of those instances.

3 Likes