我想设置一个 Terraform 模块,根据 Terraforms policy assignment example 将策略分配给 Azure 资源。 。
为了分配允许的位置策略,我想将允许的位置列表作为字符串列表从 Variables.tf 文件传递到执行分配的 main.tf。
main.tf
#Allowed Locations Policy Assignment
resource "azurerm_policy_assignment" "allowedlocations" {
name = "allowed-locations"
scope = var.scope_allowedlocations
policy_definition_id = var.policy_allowedlocations.id
description = "This policy enables you to restrict the locations."
display_name = "Allowed Locations"
parameters = <<PARAMETERS
{
"allowedLocations": {
"value": ${var.listofallowedlocations}
}
}
PARAMETERS
}
变量.tf
# Scope of the Allowed Locations policy
variable "scope_allowedlocations" {
description = "The scope of the allowed locations assignment."
default = "Subscription"
}
# Scope of the Allowed Locations policy
variable "policy_allowedlocations" {
description = "The allowed locations policy (created by the policy-define module)."
default = "default"
}
# List of the Allowed Locations
variable "listofallowedlocations" {
type = list(string)
description = "The allowed locations list."
default = [ "West Europe", "North Europe", "East US" ]
}
使用terraform plan
执行会导致以下错误:
Error: Invalid template interpolation value
on modules/policy-assign/main.tf line 16, in resource "azurerm_policy_assignment" "allowedlocations":
12:
13:
14:
15:
16: "value": ${var.listofallowedlocations}
17:
18:
19:
|----------------
| var.listofallowedlocations is list of string with 3 elements
Cannot include the given value in a string template: string required.
因此,我不知道如何准确地将列表从变量文件传递到策略分配资源的 PARAMETERS 部分。在 Terraforms 中 policy assignment example该列表直接在 PARAMETERS 部分中进行内联编码,并且可以正常工作。但没有传递变量...:
parameters = <<PARAMETERS
{
"allowedLocations": {
"value": [ "West Europe" ]
}
}
PARAMETERS
最佳答案
当您将值插入字符串时,该值本身必须可转换为字符串,否则 Terraform 无法将各个部分连接在一起以生成单个字符串结果。
这里有几种不同的替代方案,具有不同的权衡。
<小时/>我个人在这里选择的选项是不使用 <<PARAMETERS
语法并使用 jsonencode
构建整个值:
parameters = jsonencode({
allowedLocations = {
value = var.listofallowedlocations
}
})
这根本不需要您的配置来处理任何 JSON 语法问题,并且(主观上)因此使意图更加清晰,并且将来的维护更加容易。
在结果是单个有效 JSON 值的任何情况下,我总是选择使用 jsonencode
而不是模板语言。为了完整性,我在下面添加了其他选项,以防将来的读者尝试将集合值包含到不生成 JSON 的字符串模板中。
第二个选项是编写一个表达式来告诉 Terraform 将列表值转换为合适格式的字符串值的方法。在您的情况下,您需要 JSON 等 jsonencode
再次可能是最合适的选择:
parameters = <<PARAMETERS
{
"allowedLocations": {
"value": ${jsonencode(var.listofallowedlocations)}
}
}
PARAMETERS
在其他非 JSON 情况下,当结果是简单的字符串列表时,join
函数对于将所有字符串与固定分隔符连接在一起非常有用。
Terraform's functions 中的任何一个产生单个字符串的结果是这里的候选者。 “字符串函数”和“编码函数”下的函数是最有可能的选择。
<小时/>最后,对于从集合值到结果字符串的映射是自定义的、没有标准函数可以处理的情况,您可以使用模板重复语法:
parameters = <<CONFIG
%{ for port in var.ports ~}
listen 127.0.0.1:${port}
%{ endfor ~}
CONFIG
在这种情况下,Terraform 将为 var.ports
中的每个元素评估重复构造的主体一次。并将所有结果连接在一起以产生结果。您可以使用此方法生成各种文本输出格式,但如果模板变得特别复杂,最好将其分解为单独的文件并使用 templatefile
函数对其进行评估。
关于json - Terraform:将包含字符串列表的变量传递到 jsonencode 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218755/