代码之家  ›  专栏  ›  技术社区  ›  Prashanth Sams

不在地形变量内工作的局部变量

  •  0
  • Prashanth Sams  · 技术社区  · 6 年前
    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
        }
    }
    

    enter image description here

    我需要什么

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

    0 回复  |  直到 6 年前
        1
  •  0
  •   SomeGuyOnAComputer Joril    6 年前

    转换 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"
      },
    ]