简体   繁体   English

如何在 terraform 中为 for_each 循环条件获取 output?

[英]How to get output for for_each loop condition within terraform?

I have a resource block with for_each loop condition and I wanted to output name and address_prefixes of the resource block.我有一个带有 for_each 循环条件的资源块,我想要 output 资源块的名称和地址前缀。

main.tf:主文件:

resource "azurerm_subnet" "snets" {
    for_each = var.subnets
    name = each.key
    resource_group_name = azurerm_resource_group.rg.name
    virtual_network_name = azurerm_virtual_network.vnet.name
    address_prefixes = [each.value]
}

I have tried something like this, but it didn't worked.我已经尝试过这样的事情,但它没有奏效。

output.tf output.tf

output "azurerm-subnet" {
    value = azurerm_subnet.snets.*.name
}

Error:错误:

│ Error: Unsupported attribute
│
│   on output.tf line 2, in output "azurerm-subnet":
│    2:     value = azurerm_subnet.snets.*.name
│
│ This object does not have an attribute named "name".

This can most easily be accomplished with a list constructor and a for expression.这可以通过list构造函数和for表达式最轻松地完成。 We iterate through the map of exported attributes for the azurerm_subnet.snets and return the name value on each iteration:我们遍历map的导出属性的azurerm_subnet.snets并在每次迭代时返回name值:

output "azurerm_subnets" {
  value = [ for subnet in azurerm_subnet.snets : subnet.name ]
}

and the output azurerm_subnets will be a list(string) where each element is the subnet name. output azurerm_subnets将是一个list(string) ,其中每个元素都是子网名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM