繁体   English   中英

如何迭代列表(字符串)和 append 到相同的记录 Terraform

[英]How to iterate over list(string) and append to same records Terraform

我想遍历字符串列表并将单个值应用于同一资源 route53 我在变量中有 ip 列表

variable "app_list" {
description = "List of ESAs to be allowed. (For instance, \[192.168.1.123, 10.1.1.11\] etc.)"
type        = list(string)
default = \["192.168.1.123","10.1.1.11"\]
}

创建 route53 TXT 记录,我必须使用 append 这个变量并创建单个记录

resource "aws_route53_record" "spf_txt" {
zone_id = data.aws_route53_zone.public.zone_id
name = ""
type = "TXT"
ttl = 300

records = \["v=spf1 ip4:192.168.1.123 ip4:10.1.1.11 \~all"\]
}

在这里我使用了 for_each 和计数。 它试图创建两个单独的 TXT 记录。 我如何迭代列表并将其传递给记录。

请有人帮助我

试过:

resource "aws_route53_record" "spf_txt" {
zone_id = data.aws_route53_zone.public.zone_id
name = ""
type = "TXT"
ttl = 300
count = length(var.app_list)
for_each = var.app_list
records = \["v=spf1 ip4:value \~all"\]
}

它被误认为是带有元组的两个元素

也试过这个

locals {
spf_record = "${formatlist("ip4:", var.app_list)}" 
}

resource "aws_route53_record" "spf_txt" {
zone_id = data.aws_route53_zone.public.zone_id
name = "" 
type = "TXT"
ttl = 300
records = \["v=spf1 ${local.spf_record} ip4:${data.aws_nat_gateway.nat_ip.public_ip} \~all"\]
}

调用 formatlist(format, args...) 时出现此错误 spf_record = "${formatlist("ip4:", var.app_list)}" 失败 var.app_esas 是包含 2 个元素的字符串列表

调用 function“格式列表”失败:格式迭代 0 错误:太多 arguments; 格式字符串中没有动词。

我认为,即使您不使用countfor_each ,您也可以达到目的。

resource "aws_route53_record" "spf_txt" {
  ...
  records = ["v=spf1 ${join(" ", [for i in var.app_list : "ip4:${i}"])} ~all"]
}

测试:

variable "app_list" {
  description = "List of ESAs to be allowed. (For instance, [192.168.1.123, 10.1.1.11] etc.)"
  type        = list(string)
  default = ["192.168.1.123","10.1.1.11"]
}

output "spf_txt" {
  value = ["v=spf1 ${join(" ", [for i in var.app_list : "ip4:${i}"])} ~all"]
}
$ terraform plan

Changes to Outputs:
  + spf_txt = [
      + "v=spf1 ip4:192.168.1.123 ip4:10.1.1.11 ~all",
    ]

如果使用formatlist

resource "aws_route53_record" "spf_txt" {
  ...
  records = ["v=spf1 ${join(" ", formatlist("ip4:%s", var.app_list))} ~all"]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM