基于变量的可选块
Optional block based on variable
我正在为 azurerm_storage_account
编写某种包装器模块。
azurerm_storage_account
有可选块
static_website {
index_document = string
error_404_document = string
}
我想根据变量进行设置,但我不太确定该怎么做?条件运算符并不真正适用于块(例如 static_website = var.disable ? null : { .. }
)
或者块的工作方式是,如果我将 index_document
和 error_404_document
设置为 null
,它与不设置 static_website
是一样的完全阻止?
azurerm@2.x
TF@0.12.x
我认为您可以使用 dynamic block。基本上,当 disable
为 true
时,不会创建 static_website
。否则,将构建一个static_website
块。
例如,修改后的代码可以是:
dynamic "static_website" {
for_each = var.disable == true ? toset([]) : toset([1])
content {
index_document = string
error_404_document = string
}
}
您也可以尝试使用 splat 来检查 disable
是否具有值或是否为空:
dynamic "static_website" {
for_each = var.disable[*]
content {
index_document = string
error_404_document = string
}
}
在上面的示例中,您可能需要根据 var.disable
实际可以具有的值来调整条件。
我正在为 azurerm_storage_account
编写某种包装器模块。
azurerm_storage_account
有可选块
static_website {
index_document = string
error_404_document = string
}
我想根据变量进行设置,但我不太确定该怎么做?条件运算符并不真正适用于块(例如 static_website = var.disable ? null : { .. }
)
或者块的工作方式是,如果我将 index_document
和 error_404_document
设置为 null
,它与不设置 static_website
是一样的完全阻止?
azurerm@2.x
TF@0.12.x
我认为您可以使用 dynamic block。基本上,当 disable
为 true
时,不会创建 static_website
。否则,将构建一个static_website
块。
例如,修改后的代码可以是:
dynamic "static_website" {
for_each = var.disable == true ? toset([]) : toset([1])
content {
index_document = string
error_404_document = string
}
}
您也可以尝试使用 splat 来检查 disable
是否具有值或是否为空:
dynamic "static_website" {
for_each = var.disable[*]
content {
index_document = string
error_404_document = string
}
}
在上面的示例中,您可能需要根据 var.disable
实际可以具有的值来调整条件。