ansibleterraformopenstackansible-inventoryterraform-provider-openstack

Terraform create list


I try to put all floating ips of my coturn servers in a ansible inventory.

resource "local_file" "hosts_cfg" {
  content = templatefile("${path.module}/templates/hosts.tpl",
    {
      coturn = openstack_compute_floatingip_associate_v2.coturn[*].floating_ip
    }
  )
  filename = "../ansible/inventory/hosts.cfg"
}

Error: This object does not have an attribute named "floating_ip".

resource "local_file" "hosts_cfg" {
  content = templatefile("${path.module}/templates/hosts.tpl",
    {
      coturn = openstack_compute_floatingip_associate_v2.coturn["coturn-1"].floating_ip
    }
  )
  filename = "../ansible/inventory/hosts.cfg"
}

Works, but only for one instance.

My template file.

[coturn]
%{ for ip in coturn ~}
${ip}
%{ endfor ~}

My openstack_compute_floatingip_associate_v2 definition.

resource "openstack_compute_floatingip_associate_v2" "coturn" {
  for_each = var.coturn_instance_names
  floating_ip = openstack_networking_floatingip_v2.coturn[each.key].address
  instance_id = openstack_compute_instance_v2.coturn[each.key].id
}

Solution

  • This is not the answer to my question, but solves the problem.
    I currently use this configuration, mentioned from zigarn.

    resource "local_file" "hosts_cfg" {
      content = templatefile("${path.module}/templates/hosts.tpl",
        {
          coturn = openstack_compute_floatingip_associate_v2.coturn
        }
      )
      filename = "../ansible/inventory/hosts.ini"
    }
    
    [coturn]
    %{ for instance in coturn ~}
    ${instance.floating_ip}
    %{ endfor ~}
    

    I still don't know, why this works(link)

    kafka_processors = aws_instance.kafka_processor.*.public_ip
    

    but this does not

    coturn = openstack_compute_floatingip_associate_v2.coturn.*.floating_ip