azure Terraform -用于资源创建的嵌套循环

hjzp0vay  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(136)

我正在尝试在所有已定义的resource_groups中创建所有已定义的virtual_networks。
变量如下所示:

inputs = {
  virtual_networks = {
    "test-net1": {
      address_space = ["10.0.0.0/24"]
    }

    "test-net2": {
      address_space = ["10.0.1.0/24"]
    }
  }
  resource_groups = {
    "group1": {
      location  = "West Europe"
    }
    "group2": {
      location  = "East Europe"
    }
  }
}

资源块如下所示:

resource "azurerm_virtual_network" "virtual_network" {
  for_each = var.resource_groups

  name                = <FIRST KEY OF var.virtual_networks>
  resource_group_name = each.key
  location            = each.value["location"]
  address_space       = <ADDRESS_SPACE VALUE OF FIRST NETWORK FROM var.virtual_networks>

}

我尝试了许多不同的方法来转换/重组/合并变量的Map,这样我就可以用更好的方式来迭代它们,但是我就是找不到正确的结构和如何构建它。
我建立了下面的局部变量来帮助解决这个问题。但是我不能用一种方式来构造Map,这样我就可以在一个单个资源块中循环它。

locals = {
  resource_groups = {
    for key, resource_groups in var.resource_groups : "${key}" => 
    merge(
      var.virtual_networks,
      resource_groups
    )
  }
}

输出:

group1 = {
               location             = "West Europe"
               test-net1            = {
                   address_space = [
                       "10.0.0.0/24",
                    ]
                }
               test-net2 = {
                   address_space = [
                       "10.0.1.0/24",
                    ]
                }
            }
           group2  = {
               location             = "West Europe"
               test-net1            = {
                   address_space = [
                       "10.0.0.0/24",
                    ]
                }
               test-net2 = {
                   address_space = [
                       "10.0.1.0/24",
                    ]
                }
            }
piztneat

piztneat1#

据我所知,你想在两个字典上创建一个嵌套循环,你可以像这样构造你的局部:

locals {
  resource_groups = distinct(flatten([
    for k1, rg in var.resource_groups : [
      for k2, vn in var.virtual_networks : {
        group = merge({name = "${k1}"}, rg )
        network = merge({name = "${k2}"}, vn )
      }
    ]
  ]))
}

产出将导致:

Changes to Outputs:
  + test = [
      + {
          + group   = {
              + location = "West Europe"
              + name     = "group1"
            }
          + network = {
              + address_space = [
                  + "10.0.0.0/24",
                ]
              + name          = "test-net1"
            }
        },
      + {
          + group   = {
              + location = "West Europe"
              + name     = "group1"
            }
          + network = {
              + address_space = [
                  + "10.0.1.0/24",
                ]
              + name          = "test-net2"
            }
        },
      + {
          + group   = {
              + location = "East Europe"
              + name     = "group2"
            }
          + network = {
              + address_space = [
                  + "10.0.0.0/24",
                ]
              + name          = "test-net1"
            }
        },
      + {
          + group   = {
              + location = "East Europe"
              + name     = "group2"
            }
          + network = {
              + address_space = [
                  + "10.0.1.0/24",
                ]
              + name          = "test-net2"
            }
        },
    ]

您的资源将如下所示:

resource "azurerm_virtual_network" "virtual_network" {
  for_each = {for obj in local.resource_groups : "${obj.group.name}_${obj.network.name}" => obj}

  name                = each.value.network.name
  resource_group_name = each.value.group.name
  location            = each.value.group.location
  address_space       = each.value.network.address_space

}

相关问题