Terraform 创建列表

问题描述

我尝试将我的 coturn 服务器的所有浮动 ip 放在 ansible 清单中。

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"
}

错误:此对象没有名为“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"
}

有效,但仅适用于一个实例。

我的模板文件

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

我的 openstack_compute_floatingip_associate_v2 定义。

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
}

解决方法

这不是我问题的答案,而是解决了问题。
我目前使用这个配置,从 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 ~}

我还是不知道为什么会这样(link)

kafka_processors = aws_instance.kafka_processor.*.public_ip

但这没有

coturn = openstack_compute_floatingip_associate_v2.coturn.*.floating_ip