我想使用通用模块为域创建每种类型的DNS记录,因此可以通过以下方式对其进行调用:
module "example_com_dns" {
source = "[PATH_TO_MODULES]/modules/dns"
domain = "example.com"
a_records = {
"@" = [SOME IP]
"www" = [SOME IP]
"home" = [SOME IP]
}
txt_records = {
"@" = "txt-foobar1"
"@" = "txt-foobar2"
"mail._domainkey.self" = "foobar"
}
mx_entries = {
"10" = "mail.someprovider.com"
"20" = "mail2.someprovider.com"
}
cname_records {
"cname-foo" = "cname-bar
}
}
对于A,CNAME和MX记录,我有一些很好的方法,但是TXT有一个边缘情况,我需要解决。我的模块具有每种类型的记录的资源块,这些资源块通过循环运行。我将粘贴TXT,但它们都是相同的:
resource "digitalocean_record" "this_txt_record" {
for_each = var.txt_records
domain = var.domain
type = "TXT"
name = each.key
value = each.value
}
这一切都很好,除了以下事实之外:由于有2条记录的键带有“ @”,因此只创建了最后一条(在上面的示例中为“ txt-foobar2”):
...
# module.example_com.digitalocean_record.this_txt_record["@"] will be created
+ resource "digitalocean_record" "this_txt_record" {
+ domain = "example.com"
+ fqdn = (known after apply)
+ id = (known after apply)
+ name = "@"
+ ttl = (known after apply)
+ type = "TXT"
+ value = "txt-foobar2"
}
我希望它同时创建“ txt-foobar1”和“ txt-foobar2”,即使在地图中指定了非唯一键也是如此。
也许这是错误的方式,我只需要找出一个聪明的循环来解析此结构?
txt_records = [
{ "@" = "foo" },
{ "@" = "bar"},
{ "mail._domainkey.self" = "foobar"}
]
如果是这样,我目前也在那儿失败了:)
已经给出的一种替代方法是使用以下方法:
variable "txt_records" {
default = {
"@" = ["foo", "bar"],
"mail._domainkey.self" = ["foobar"]
}
}
然后,您可以展平txt_records
使用:
locals {
txt_records_flat = merge([
for key, values in var.txt_records:
{for value in values:
"${key}-${value}" => {"record_name" = key, "record_value" = value}
}
]...)
}
结果local.txt_records_flat
是:
{
"@-bar" = {
"record_name" = "@"
"record_value" = "bar"
}
"@-foo" = {
"record_name" = "@"
"record_value" = "foo"
}
"mail._domainkey.self-foobar" = {
"record_name" = "mail._domainkey.self"
"record_value" = "foobar"
}
}
然后使用它:
resource "digitalocean_record" "this_txt_record" {
for_each = local.txt_records_flat
domain = var.domain
type = "TXT"
name = each.value.record_name
value = each.value.record_value
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句