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

时间:2019-07-26 11:03:54

标签: json azure variable-assignment terraform policy

我想设置一个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

}

variables.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
{
  "allowedLocations": {
    "value": [ "West Europe" ]
  }
}
PARAMETERS

2 个答案:

答案 0 :(得分:0)

你能尝试

====================[ Build | all | Debug ]=====================================
/opt/clion/bin/cmake/linux/bin/cmake --build /home/assembly/CLionProjects    /playground/cmake-build-debug --target all -- -j 2
Scanning dependencies of target playground
[ 33%] Building C object CMakeFiles/playground.dir/main.c.o
[ 66%] Linking C executable playground
[100%] Built target playground

Build finished

答案 1 :(得分:0)

将值插值到字符串中时,该值本身必须可以转换为字符串,否则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函数对其进行评估。