Hello, I need some help. I don’t understand how I can link iteration between resources
I have a map :
project = {
vm1 = {
hostname = "example1",
address = "192.168.1.1"
},
vm2 = {
hostname = "example2",
address = "192.168.1.2"
}
}
Then a vm:
resource "vsphere_virtual_machine" "vm" {
for_each = var.project
name = each.key
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_cluster_id = data.vsphere_datastore_cluster.datastore_cluster.id
num_cpus = 4
memory = 8196
guest_id = "centos7_64Guest"
folder = "Testing"
scsi_type = "pvscsi"
disk {
label = "disk0"
unit_number = 0
size = 8
eagerly_scrub = false
thin_provisioned = false
}
network_interface {
network_id = data.vsphere_network.network_internal.id
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
linux_options {
host_name = each.value.hostname
domain = "localhost.domain"
}
network_interface {}
}
}
extra_config = {
"guestinfo.metadata" = base64gzip(file("${path.module}/cloudinit/metadata.yaml"))
"guestinfo.metadata.encoding" = "gzip+base64"
"guestinfo.userdata" = base64gzip(file("${path.module}/cloudinit/userdata.yaml"))
"guestinfo.userdata.encoding" = "gzip+base64"
}
}
then a template_file that customize metadata.yaml. My goal is when iterating over vm, the template_file.vm will dynamically set value in metadata.yaml. So that each vm will be set up with its ip and hostname, using the same map
data "template_file" "metadata" {
for_each = vsphere_virtual_machine.vm
template = file("${path.module}/cloudinit/metadata.yaml")
vars = {
hostname = each.value.hostname
address = each.value.address-internal
}
}
I need to be able to retrieve the map that was applied to vsphere_virtual_machine.vm to be able to define correctly hostname and address in the template_file.
Thanks in advance.