Using element() in output

Hi,

I cannot get element() to work in mf tf output.

output "kube_hosts" {
  description = "Control plane endpoints to SSH to"

  value = {
    control_plane = {
      cluster_name         = var.cluster_name
      cloud_provider       = "vsphere"
      private_address      = []
      public_address       = element(vsphere_virtual_machine.control_plane.*.guest_ip_addresses, 1)
      ssh_agent_socket     = var.ssh_agent_socket
      ssh_port             = var.ssh_port
      ssh_private_key_file = var.ssh_private_key_file
      ssh_user             = var.ssh_username
    }
  }
}

Am I doing something stupid?

kube_hosts = {
  "control_plane" = {
    "cloud_provider" = "vsphere"
    "cluster_name" = "testing-2"
    "private_address" = []
    "public_address" = [
      "10.2.2.20",
      "10.1.32.211",
      "fe80::250:56ff:fead:f818",
      "fe80::250:56ff:fead:72c1",
    ]
    "ssh_agent_socket" = "env:SSH_AUTH_SOCK"
    "ssh_port" = 22
    "ssh_private_key_file" = "testing-2/id_rsa"
    "ssh_user" = "ubuntu"
  }
}

I am expecting to only see one public_address - the 2nd entry.

Cheers,

Andrew

Hi @mooperd,

vsphere_virtual_machine.control_plane.*.guest_ip_addresses produces a list of the lists of IP addresses for each of your virtual machines, so your full expression is taking one element of that list, whose result is a list of IP addresses for one of your machines.

To select only a single IP address would require two list element lookups: one to select a virtual machine, and then the second to select an IP address from that machine. Something like this:

vsphere_virtual_machine.control_plane[1].guest_ip_addresses[1]