Terraform:将包含字符串列表的变量传递给 jsonencode 部分

Pet*_*ter 5 policy json variable-assignment azure terraform

我想设置一个 Terraform 模块,根据 Terraforms策略分配示例策略分配给 Azure 资源。

为了分配允许位置策略,我想将允许位置列表作为字符串列表从 variables.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

}
Run Code Online (Sandbox Code Playgroud)

变量.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" ]
}
Run Code Online (Sandbox Code Playgroud)

执行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.
Run Code Online (Sandbox Code Playgroud)

因此,我不知道如何将列表从变量文件准确地传递到策略分配资源的 PARAMETERS 部分。在 Terraforms策略分配示例中,列表直接在 PARAMETERS 部分中进行了内联编码,并且可以正常工作。但是没有传递变量...:

parameters = <<PARAMETERS
{
  "allowedLocations": {
    "value": [ "West Europe" ]
  }
}
PARAMETERS
Run Code Online (Sandbox Code Playgroud)

Mar*_*ins 11

将值插入字符串时,该值本身必须可转换为字符串,否则 Terraform 无法将各个部分连接在一起以生成单个字符串结果。

这里有几种不同的选择,具有不同的权衡。


我个人在这里选择的选项是根本不使用<<PARAMETERS语法,而只使用jsonencode以下方法构建整个值:

parameters = jsonencode({
  allowedLocations = {
    value = var.listofallowedlocations
  }
})
Run Code Online (Sandbox Code Playgroud)

这完全避免了您的配置需要处理任何 JSON 语法问题,因此(主观上)使意图更清晰,未来维护更容易。

在结果是单个有效 JSON 值的任何情况下,我总是选择使用jsonencode而不是模板语言。为了完整起见,我将包含下面的其他选项,以防将来的读者尝试将集合值包含到生成 JSON的字符串模板中。


第二种选择是编写一个表达式来告诉 Terraform 一种将列表值转换为合适格式的字符串值的方法。在您的情况下,您需要 JSON,因此jsonencode可能是最合适的选择:

parameters = <<PARAMETERS
{
  "allowedLocations": {
    "value": ${jsonencode(var.listofallowedlocations)}
  }
} 
PARAMETERS
Run Code Online (Sandbox Code Playgroud)

在其他非 JSON 情况下,当结果是一个简单的字符串列表时,该join函数可用于将所有字符串与固定分隔符连接在一起。

任何生成单个字符串的Terraform 函数都是这里的候选函数。“字符串函数”和“编码函数”下的那些是最有可能的选择。


最后,对于从集合值到结果字符串的映射是标准函数无法处理的自定义内容的情况,您可以使用模板重复语法:

parameters = <<CONFIG
%{ for port in var.ports ~}
listen 127.0.0.1:${port}
%{ endfor ~}
CONFIG
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Terraform 将对中的每个元素对重复构造的主体进行一次评估,var.ports并将所有结果连接在一起以产生结果。您可以使用这种方法生成各种文本输出格式,但如果模板变得特别复杂,最好将其分解为单独的文件并使用该templatefile函数对其进行评估。