[英]Can you use the lookup function in terraform with map variables containing arrarys?
[英]Map within a map in terraform variables
有誰知道是否有可能用代碼片段來表示我是否可以在 terraform 變量的地圖變量中創建地圖變量?
variable "var" {
type = map
default = {
firstchoice = {
firstAChoice ="foo"
firstBChoice = "bar"
}
secondchoice = {
secondAChoice = "foobar"
secondBChoice = "barfoo"
}
}
}
如果有人對這是否可行或任何詳細說明的文檔有任何見解,那就太好了。
是的,可以將地圖變量作為地圖變量鍵的值。 您的變量只需要正確的縮進。 此外,我正在考慮訪問該變量的方法。
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 預覽版:豐富的值類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.