检查变量是否存在 - Terraform 模板语法

Check if variable exists - Terraform template syntax

我正在尝试使用 Terraform 模板语法检查模板文件中是否存在变量,但出现 This object does not have an attribute named "proxy_set_header.

错误
$ cat nginx.conf.tmpl

%{ for location in jsondecode(locations) }
location ${location.path} {
    %{ if location.proxy_set_header }
       proxy_set_header ${location.proxy_set_header};
    %{ endif }
}
%{ endfor }

我尝试了 if location.proxy_set_header != ""if location.proxy_set_header 但没有成功。

如何使用字符串模板检查变量是否存在?

我会做类似下面的事情,使用 containskeys

%{ for location in jsondecode(locations) }
location ${location.path} {
    %{ if contains(keys(location), "proxy_set_header") }
       proxy_set_header ${location.proxy_set_header};
    %{ endif }
}
%{ endfor }

解析后的JSON本质上变成了map,可以检查关键内容。

我用下面的代码测试了这个

data "template_file" "init" {
  template = file("${path.module}/file.template")
  vars = {
    locations = <<DOC
[
  {
    "path": "foo",
    "proxy_set_header": "foohdr"
  },
  {
    "path": "bar"
  }
]
DOC
  }
}

output "answer" {
  value = data.template_file.init.rendered
}

它有以下输出

Outputs:

answer = 
location foo {

       proxy_set_header foohdr;

}

location bar {

}

如果您使用的是 Terraform 0.12.20 或更高版本,那么您可以使用新函数 can 来简洁地编写如下检查:

%{ for location in jsondecode(locations) }
location ${location.path} {
    %{ if can(location.proxy_set_header) }
       proxy_set_header ${location.proxy_set_header};
    %{ endif }
}
%{ endfor }

can 函数 returns 如果给定表达式的计算没有错误则为真。


文档确实建议在大多数情况下首选 try,但在这种特殊情况下,如果该属性不存在,您的目标是完全不显示任何内容,因此这种与 [=14= 等效的方法] 我认为,未来更难理解 reader:

%{ for location in jsondecode(locations) }
location ${location.path} {
    ${ try("proxy_set_header ${location.proxy_set_header};", "") }
}
%{ endfor }

除了(主观上)对意图更加不透明之外,这忽略了 try 文档中关于仅将其与属性查找和类型转换表达式一起使用的建议。因此,我认为上面的 can 用法是合理的,因为它相对清晰,但无论哪种方式都应该有效。