输出的地形条件
Terraform conditions for Output
子网名称将根据 env 是非 prod 还是 prod 而有所不同。
不能同时使用基于计数和 for_each 的引用错误。
需要以下两项之一:
仅当 env 是 prod 时才执行资源块?
或者 output.tf 如果未从资源中找到值则忽略错误。
resource "azurerm_subnet" "subnet" {
count = var.environment == "prd" ? 1 : 0 //execute block only if env is prod
for_each = var.subnet_names
name = join("",["test-","${var.environment}",each.key,"-snet"])
resource_group_name = azurerm_virtual_network.vnet.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = tolist(each.value)
}
Variables.tf
variable "subnet_names" {
description = "subnet names"
default = {}
type = map
}
variable "environment" {
description = "Name of prod environment"
default = "prd"
}
tf变量:
subnet_names = {
"s1-uat" = ["20.0.0.0/27"] ,
"s1-qa" = ["20.0.0.32/27"] ,
"s2-qa" = ["20.0.0.64/27"] ,
"s2-uat" = ["20.0.0.96/27"]
}
environment = "prd"
output.tf
"s1_subnet_name" {
value = azurerm_subnet.subnet["s1-uat"].name
depends_on = [
azurerm_subnet.subnet
]
}
错误:
“count”和“for_each”元参数是互斥的,只有一个
应该用于明确要创建的资源数量。
您只能使用计数或 for_each。所以你可以试试这个:
resource "azurerm_subnet" "subnet" {
count = var.environment == "prd" ? length(var.subnet_names) : 0
name = join("",["test-","${var.environment}",each.key,"-snet"])
resource_group_name = azurerm_virtual_network.vnet.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = tolist(each.value)
}
对于名称和 address_prefixes 而不是使用 'each' 您可以使用 count.index 。在 terraform count 中不支持 each
子网名称将根据 env 是非 prod 还是 prod 而有所不同。 不能同时使用基于计数和 for_each 的引用错误。
需要以下两项之一: 仅当 env 是 prod 时才执行资源块? 或者 output.tf 如果未从资源中找到值则忽略错误。
resource "azurerm_subnet" "subnet" {
count = var.environment == "prd" ? 1 : 0 //execute block only if env is prod
for_each = var.subnet_names
name = join("",["test-","${var.environment}",each.key,"-snet"])
resource_group_name = azurerm_virtual_network.vnet.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = tolist(each.value)
}
Variables.tf
variable "subnet_names" {
description = "subnet names"
default = {}
type = map
}
variable "environment" {
description = "Name of prod environment"
default = "prd"
}
tf变量:
subnet_names = {
"s1-uat" = ["20.0.0.0/27"] ,
"s1-qa" = ["20.0.0.32/27"] ,
"s2-qa" = ["20.0.0.64/27"] ,
"s2-uat" = ["20.0.0.96/27"]
}
environment = "prd"
output.tf
"s1_subnet_name" {
value = azurerm_subnet.subnet["s1-uat"].name
depends_on = [
azurerm_subnet.subnet
]
}
错误: “count”和“for_each”元参数是互斥的,只有一个 应该用于明确要创建的资源数量。
您只能使用计数或 for_each。所以你可以试试这个:
resource "azurerm_subnet" "subnet" {
count = var.environment == "prd" ? length(var.subnet_names) : 0
name = join("",["test-","${var.environment}",each.key,"-snet"])
resource_group_name = azurerm_virtual_network.vnet.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = tolist(each.value)
}
对于名称和 address_prefixes 而不是使用 'each' 您可以使用 count.index 。在 terraform count 中不支持 each