[英]Output of values from multiple subnets created using a for_each loop
I've created some subnets using a for_each loop like below我已经使用如下的 for_each 循环创建了一些子网
main.tf in child module子模块中的 main.tf
resource "azurerm_subnet" "obc_subnet" {
name = var.subnet_name
address_prefixes = var.address_prefixes
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
}
main.tf in root module根模块中的 main.tf
module "deploy_subnet" {
source = "./modules/azure_subnet"
for_each = var.prefix
subnet_name = each.value["name"]
address_prefixes = [each.value["cidr"]]
subscription_id = var.subscription_id
resource_group_name = var.resource_group_name
region = var.region
vnet_name = var.vnet_name
}
variables.tf in root module根模块中的 variables.tf
variable "prefix" {
type = map(object({
name = string
cidr = string
}))
default = {
sub-1 = {
name = "aks-sn"
cidr = "10.0.1.0/24"
}
sub-2 = {
name = "postgres-sn"
cidr = "10.0.2.0/24"
}
sub-3 = {
name = "keyvault-sn"
cidr = "10.0.3.0/24"
}
}
}
Thats all working fine except when I try and output the values associated with the 3 subnets (id for example)一切正常,除非我尝试 output 与 3 个子网关联的值(例如 id)
I'm trying to use the for k syntax in the output but I'm not really understanding how it should look.我正在尝试在 output 中使用 for k 语法,但我并不真正了解它的外观。
I have the below in my output.tf of the child module我在子模块的 output.tf 中有以下内容
output "subnet_ids" {
value = tomap({
for k, subnets in azurerm_subnet.obc_subnet : k => subnets.id
})
}
I wasnt really sure how to approach the output.tf in my root module so I left it as is我真的不知道如何在我的根模块中处理 output.tf 所以我把它留在原样
output "subnet_id" {
value = module.deploy_subnet.subnet_id
}
I'm getting an error that the value has no attributes我收到一个错误,该值没有属性
Error:
Unsupported attribute
on modules/azure_subnet/output.tf line 3, in output "subnets_id":
3: for k, subnets in azurerm_subnet.obc_subnet : k => subnets.is
This value does not have any attributes.
Close, but the other way around.关闭,但反过来。
The azurerm_subnet
resource in the child module is not a collection, so the output there should be just:子模块中的
azurerm_subnet
资源不是一个集合,所以那里的 output 应该只是:
output "subnet_id" {
value = azurerm_subnet.obc_subnet.id
}
But in the root module you have the collection of child modules.但是在根模块中,您有子模块的集合。 Thus for example:
例如:
output "subnet_ids" {
value = {
for k, subnet in module.deploy_subnet : k => subnet.subnet_id
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.