use*_*142 1 terraform terraform-provider-azure
我有一个变量var.delete_retention_policy_days。如果该变量设置为,0那么我不希望启用保单天数块。有没有一种巧妙的方法可以在地形中做到这一点?
delete_retention_policy {
days = var.delete_retention_policy_days
}
Run Code Online (Sandbox Code Playgroud)
这是完整资源的示例
resource "azurerm_storage_account" "example2" {
name = var.azurerm_storage_account_name
resource_group_name = azurerm_resource_group.parameters.name
location = azurerm_resource_group.parameters.location
account_tier = var.azurerm_storage_account_account_tier
account_replication_type = var.azurerm_storage_account_account_replication_type
allow_blob_public_access = var.azurerm_storage_account_allow_blob_public_access
blob_properties {
delete_retention_policy {
days = var.delete_retention_policy_days
}
versioning_enabled = true
change_feed_enabled = true
}
Run Code Online (Sandbox Code Playgroud)
有条件地配置资源内的块的能力仍然是一个突出的功能请求。对于请求此功能的 Github 问题,内部开发团队提供了以下算法作为当前的解决方法:
dynamic "<block name>" {
for_each = range(<conditional> ? 1 : 0)
content {
...
}
}
Run Code Online (Sandbox Code Playgroud)
对于delete_retention_policy_days已经是一种 number类型的特定情况,这变得稍微简单一些:
dynamic "delete_retention_policy" {
for_each = range(length(var.delete_retention_policy_days) > 0 ? 1 : 0)
content {
days = var.delete_retention_policy_days
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,对于利用元参数的条件功能,还有一些更有趣的函数for_each:
# useful for objects with optional keys to iterate on them only if they exist
for_each = try(var.value, [])
# useful for objects with optional keys to conditionally configure a block only if they exist
for_each = range(can(var.value) ? 1 : 0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2410 次 |
| 最近记录: |