如何在terraform中使用locals来重复和合并块?

时间:2017-12-26 02:11:53

标签: syntax terraform

我有多个docker_container资源:

resource "docker_container" "headerdebug" {
  name  = "headerdebug"
  image = "${docker_image.headerdebug.latest}"

  labels {
    "traefik.frontend.rule" = "Host:debug.in.bb8.fun"
    "traefik.port" = 8080
    "traefik.enable" = "true"
    "traefik.frontend.passHostHeader" = "true"
    "traefik.frontend.headers.SSLTemporaryRedirect" = "true"
    "traefik.frontend.headers.STSSeconds" = "2592000"
    "traefik.frontend.headers.STSIncludeSubdomains" = "false"
    "traefik.frontend.headers.customResponseHeaders" = "${var.xpoweredby}"
    "traefik.frontend.headers.customFrameOptionsValue" = "${var.xfo_allow}"
  }
}

还有一个:

resource "docker_container" "cadvisor" {
  name  = "cadvisor"
  image = "${docker_image.cadvisor.latest}"

  labels {
    "traefik.frontend.rule" = "Host:cadvisor.bb8.fun"
    "traefik.port" = 8080
    "traefik.enable" = "true"
    "traefik.frontend.headers.SSLTemporaryRedirect" = "true"
    "traefik.frontend.headers.STSSeconds" = "2592000"
    "traefik.frontend.headers.STSIncludeSubdomains" = "false"
    "traefik.frontend.headers.contentTypeNosniff" = "true"
    "traefik.frontend.headers.browserXSSFilter" = "true"
    "traefik.frontend.headers.customFrameOptionsValue" = "${var.xfo_allow}"
    "traefik.frontend.headers.customResponseHeaders" = "${var.xpoweredby}"
  }
}

我尝试使用locals重新使用两个容器之间的公共标签。我有以下本地定义:

locals {
  traefik_common_labels {
    "traefik.frontend.passHostHeader" = "true"
    "traefik.frontend.headers.SSLTemporaryRedirect" = "true"
    "traefik.frontend.headers.STSSeconds" = "2592000"
    "traefik.frontend.headers.STSIncludeSubdomains" = "false"
    "traefik.frontend.headers.customResponseHeaders" = "${var.xpoweredby}"
    "traefik.frontend.headers.customFrameOptionsValue" = "${var.xfo_allow}"
  }
}

但是the documentation没有提及如何使用本地合并整个块,只有地图。

我尝试过以下方法:

labels "${merge(
    local.traefik_common_labels,
    map(
      "traefik.frontend.rule", "Host:debug.in.bb8.fun",
      "traefik.port", 8080,
      "traefik.enable", "true",
    )
  )}"

会出现以下错误:

tf11计划

Error: Failed to load root config module: Error loading modules: module docker: Error parsing .terraform/modules/2f3785083ce0d0ac2dd3346cf129e795/main.tf: key 'labels "${merge(
    local.traefik_common_labels,
    map(
      "traefik.frontend.rule", "Host:debug.in.bb8.fun",
      "traefik.port", 8080,
      "traefik.enable", "true",
    )
  )}"' expected start of object ('{') or assignment ('=')

我对此PR的尝试非常不同:https://git.captnemo.in/nemo/nebula/pulls/4/files

1 个答案:

答案 0 :(得分:5)

您需要将值分配给labels,如此

locals {
  traefik_common_labels {
    "traefik.frontend.passHostHeader"                  = "true"
    "traefik.frontend.headers.SSLTemporaryRedirect"    = "true"
    "traefik.frontend.headers.STSSeconds"              = "2592000"
    "traefik.frontend.headers.STSIncludeSubdomains"    = "false"
    "traefik.frontend.headers.customResponseHeaders"   = "${var.xpoweredby}"
    "traefik.frontend.headers.customFrameOptionsValue" = "${var.xfo_allow}"
  }
}

resource "docker_container" "cadvisor" {
  name  = "cadvisor"
  image = "${docker_image.cadvisor.latest}"

  labels = "${merge(
    local.traefik_common_labels,
    map(
      "traefik.frontend.rule", "Host:debug.in.bb8.fun",
      "traefik.port", 8080,
      "traefik.enable", "true",
    ))}"
}