简体   繁体   English

terraform 中的每个循环

[英]For-each loop in terraform

I have map of values which results the name of vnet and its resource group, I need to loop them in the data section of azure resource to fetch them dynamically我有 map 的值导致 vnet 及其资源组的名称,我需要在 azure 资源的数据部分中循环它们以动态获取它们

vnetID = ([for vnets in lookup(data.azurerm_resources.spokes, "resources", []) : lookup(vnets, "id")])
vnetinfo = { for details in local.vnetID: element(slice( split("/",details), 8, 9),0)  => {
                                                                                  "rg"    : element(slice( split("/",details), 4, 5),0),
                                                                                  "vnet"  : element(slice( split("/",details), 8, 9),0)
                                                            }
  }

The vnet info block outputs the following vnet 信息块输出以下内容

"dev1-net" = {
    "rg" = "dev1-net-rg"
    "vnet" = "dev1-net"
  }
  "net-01" = {
    "rg" = "net-01-rg"
    "vnet" = "net-01"
  }
  "net-02" = {
    "rg" = "net-02-rg"
    "vnet" = "net-02"
  }

In the below block the name of the vnet and resource group should get looped from the above map of values在下面的块中,vnet 和资源组的名称应该从上面的 map 值循环

data "azurerm_virtual_network" "peeredvnets" {  
  foreach             = tomap(local.vnetinfo)
  name                = each.value["vnet"]
  resource_group_name = each.value["rg"]
}

I am doing the foreach syntax wrongly.我做错了 foreach 语法。 Can someone help on how to achieve this.有人可以帮助如何实现这一目标。 Need the name of vnet and its corresponding rg name using forloop in the above data loop在上述数据循环中使用forloop需要vnet的名称及其对应的rg名称

The error message will help, but could be for_each and not foreach错误消息会有所帮助,但可能是for_each而不是foreach

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

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