This topic is the copy of https://github.com/dmacvicar/terraform-provider-libvirt/issues/745
I have
Terraform v0.12.24
+ provider.libvirt (unversioned)
+ provider.local v1.4.0
+ provider.template v2.1.2
I use a template to generate an ansible’s inventory from the terraform.
locals {
ips = libvirt_domain.k8s.*.network_interface.0.addresses
names = libvirt_domain.k8s.*.network_interface.0.hostname
inventory = templatefile("${path.module}/inventory.tpl",
{
nodes = zipmap(local.names,
flatten(local.ips))
})
}
# generate inventory file for Ansible
resource "local_file" "inventory" {
content = local.inventory
filename = "${path.module}/../kubespray/inventory/mycluster/inventory.ini"
depends_on = [libvirt_domain.k8s]
}
And it works only if I run terraform apply
twice. After first run (I use an output to debug) in the local.inventory I got only
nodes = {
"" = "10.0.1.95"
}
and after second I see the proper result:
nodes = {
"k8s0" = "10.0.1.253"
"k8s1" = "10.0.1.190"
"k8s2" = "10.0.1.95"
}
Interesting that the problem reveals only if I use zipmap
and flatten
functions. (I need them to get proper data for a template). If I use the regular value as
output "ips" {
value = libvirt_domain.k8s.*.network_interface.0.addresses
}
It provides all 3 IPs from the first run. I’m very confused about that behaviour.