【问题标题】:locals not working within variables in Terraform本地人不在 Terraform 的变量中工作
【发布时间】:2020-04-13 21:46:03
【问题描述】:
locals {
  cool_style = {
    palette = "cool"
    type    = "solid"
    width   = "normal"
  }
  orange_style = {
    palette = "orange"
    type    = "solid"
    width   = "normal"
  }
}

variable "query_timeseries" {
  default = [
    {
      q     = "avg:xx.xxxx{xxx:xx}"
      type  = "bars"
      style = local.cool_style
    },
    {
      q     = "avg:xx.xxxx{xxx:xx}"
      type  = "bars"
      style = local.orange_style
    }
}

无法重用 .tf 文件中声明的变量

我需要什么?:

我需要通过在其中注入一个现有变量来声明一个变量

【问题讨论】:

  • 您以错误的方式使用它们。变量不能包含插值,所以如果你想要插值,那么它们应该是本地的。在您的情况下,如果 cool_styleorange_style 意味着不可配置(因此代码调用者无法更改),那么它应该都是本地人。
  • 我做了 locals.cool_style,结果是一样的
  • 没有。关键是您的query_timeseries 应该是本地人。目前您将其作为变量,但变量不能包含插值,因为错误消息告诉您。
  • 是的,我明白了;顺便说一句,它不允许我将 query_timeseries 声明为本地人。无论如何要声明cool_style & orange_style 并将其重新用作variables.tf 中的变量?
  • 这听起来真的是一个单独的问题。

标签: terraform


【解决方案1】:

variable 转换为local 以便它可以使用其他局部变量是正确的解决方案。使用 terraform 0.12.x 可以按terraform apply 所见的预期工作。

locals {
  cool_style = {
    palette = "cool"
    type    = "solid"
    width   = "normal"
  }
  orange_style = {
    palette = "orange"
    type    = "solid"
    width   = "normal"
  }
  query_timeseries = [
    {
      q     = "avg:xx.xxxx{xxx:xx}"
      type  = "bars"
      style = local.cool_style
    },
    {
      q     = "avg:xx.xxxx{xxx:xx}"
      type  = "bars"
      style = local.orange_style
    }
  ]
}

output "query_timeseries" {
  value = local.query_timeseries
}
$ terraform init
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

query_timeseries = [
  {
    "q" = "avg:xx.xxxx{xxx:xx}"
    "style" = {
      "palette" = "cool"
      "type" = "solid"
      "width" = "normal"
    }
    "type" = "bars"
  },
  {
    "q" = "avg:xx.xxxx{xxx:xx}"
    "style" = {
      "palette" = "orange"
      "type" = "solid"
      "width" = "normal"
    }
    "type" = "bars"
  },
]

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 2022-01-25
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2019-12-27
    相关资源
    最近更新 更多