地图中的地图中的地图中的地图



有人知道是否可以使用代码剪接代表我是否可以在Terraform变量中的MAP变量中创建映射变量?

variable "var" {
  type = map
  default = {
    firstchoice = {
      firstAChoice ="foo"
      firstBChoice = "bar"
    }
    secondchoice = {
      secondAChoice = "foobar"
      secondBChoice = "barfoo"
    }
  }
}

如果有人对这是否可能有任何见解或详细说明的任何文档。

是的,可以将MAP变量作为MAP变量键的值。您的变量只需要正确的凹痕。我也在介绍访问该变量的方法。

variable "var" {
  default = {
    firstchoice = {
      firstAChoice = "foo"
      firstBChoice = "bar"
    }
    secondchoice = {
      secondAChoice = "foobar"
      secondBChoice = "barfoo"
    }
  }
}

要访问地图键firstchoice的整个地图值,您可以尝试以下

value = "${var.var["firstchoice"]}"
output:
{
  firstAChoice = foo
  firstBChoice = bar
}

要访问该地图密钥的特定键(示例firstAChoice(,您可以尝试

value = "${lookup(var.var["firstchoice"],"firstAChoice")}"
output: foo

这个语法可以吗?$ {var.var [firstChoice [firstachoice]]}

带有Terraform 0.12 嵌套块的

无缝支撑。扩展@Avichal Badaya的答案,以示例来解释它:

# Nested Variable
variable "test" {
  default = {
    firstchoice = {
      firstAChoice = "foo"
      firstBChoice = "bar"
    }
    secondchoice = {
      secondAChoice = "foobar"
      secondBChoice = "barfoo"
    }
    thirdchoice = {
      thirdAChoice = {
          thirdBChoice = {
              thirdKey = "thirdValue"
        }
      }
    }
  }
}

# Outputs
output "firstchoice" {
  value = var.test["firstchoice"]
}
output "FirstAChoice" {
  value = var.test["firstchoice"]["firstAChoice"]
}
output "thirdKey" {
  value = var.test["thirdchoice"]["thirdAChoice"]["thirdBChoice"]["thirdKey"]
}

应用上述内容,您可以验证Terraform地图嵌套现在非常强大,这使很多事情变得更加容易。

# Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
# Outputs:
firstchoice = {
  "firstAChoice" = "foo"
  "firstBChoice" = "bar"
}
thirdKey = thirdValue 

有关更复杂的结构和丰富的价值类型,请参见Hashicorp Terraform 0.12预览:丰富的值类型

相关内容

  • 没有找到相关文章

最新更新