在这种情况下,您应该将 A 记录放入 list 中,而不是使用 keys 来获取 map keys:
a_records = [
["@" , "5.6.7.8"],
["@" , "7.6.7.8"],
["@" , "9.9.9.9"],
["self", "7.10.11.12"],
["self", "11.11.111.11"],
["self", "12.12.12.12"],
["self", "13.13.13.13"],
["www" , "14.14.14.14"]
]
并将 name 和 value 指定为
name = "${element(var.a_records[count.index],0)}"
value = "${element(var.a_records[count.index],1)}"
你会得到你想要的所有重复的A记录
以下是一个简单的工作示例,说明如何执行此操作。您可以将代码剪切并粘贴到功能性 terraform 会话中,它将正常运行并创建重复的 A 记录。
您需要扩展示例以满足您对外部模块等的需求。
variable "a_records" { default = [
["@" , "5.6.7.8"],
["@" , "7.6.7.8"],
["@" , "9.9.9.9"],
["self", "7.10.11.12"],
["self", "11.11.111.11"],
["self", "12.12.12.12"],
["self", "13.13.13.13"],
["www" , "14.14.14.14"],
["www" , "14.14.14.14"]
]
}
variable domain { default = "example20180731.com" }
variable main_ip { default = "1.1.1.1" }
resource "digitalocean_domain" "this_domain" {
name = "${var.domain}"
ip_address = "${var.main_ip}"
}
resource "digitalocean_record" "this_a_record" {
depends_on = ["digitalocean_domain.this_domain"]
count = "${length(var.a_records)}"
domain = "${var.domain}"
type = "A"
name = "${element(var.a_records[count.index],0)}"
value = "${element(var.a_records[count.index],1)}"
}
这是另一个例子。它展示了如何使用通过模块提供的外部数据来做到这一点。它也是可以复制并粘贴到功能性 terraform 会话中的代码,并且可以按原样工作。
create-dup-A-records.tf
module "dns" {
source = "./modules/dns"
}
variable domain { default = "example20180731.com" }
variable main_ip { default = "1.1.1.1" }
resource "digitalocean_domain" "this_domain" {
name = "${var.domain}"
ip_address = "${var.main_ip}"
}
resource "digitalocean_record" "this_a_record" {
depends_on = ["digitalocean_domain.this_domain"]
count = "${length(module.dns.a_records_in_dns_module)}"
domain = "${var.domain}"
type = "A"
name = "${element(module.dns.a_records_in_dns_module[count.index],0)}"
value = "${element(module.dns.a_records_in_dns_module[count.index],1)}"
}
./modules/dns/dns.tf
variable "a_records" { default = [
["@" , "5.6.7.8"],
["@" , "7.6.7.8"],
["@" , "9.9.9.9"],
["self", "7.10.11.12"],
["self", "11.11.111.11"],
["self", "12.12.12.12"],
["self", "13.13.13.13"],
["www" , "14.14.14.14"],
["www" , "14.14.14.14"]
]
}
output "a_records_in_dns_module" {
value = "${var.a_records}"
}