如何在 Terraform 中获取标记的 Azure VNet 的活动地址空间? [英] How can I get active address space of tagged Azure VNets inside Terraform?

查看:18
本文介绍了如何在 Terraform 中获取标记的 Azure VNet 的活动地址空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 Terraform 获得具有特定标记的 Terraform 中用于 Azure 的 VNet 的活动地址空间.为此,我认为我可以将 Resource 数据源用于虚拟网络:

I would like to get with Terraform the active address space of VNets for Azure in Terraform that have a certain tag. For this I thought I could use Resource data source for virtual networks:

data "azurerm_resources" "vnets"{
  type = "Microsoft.Network/virtualNetworks"
  required_tags = {
    tag_name = "tag"
  }
}

然后我意识到必需的属性address_space";实际上属于虚拟网络数据源(https://www.terraform.io/docs/providers/azurerm/r/virtual_network.html).我仍然需要从资源数据源中获取有关现有虚拟网络的信息.所以我尝试嵌套数据源,但是下面的代码不起作用:

Then I realized that required attribute "address_space" belongs actually to the Virtual Networks Data Source (https://www.terraform.io/docs/providers/azurerm/r/virtual_network.html). Still I need to get the information about existing virtual networks from the Resources Data Source. So I tried nesting the data sources, but the following code does not work:

data "azurerm_virtual_network" "vnets"{
  for_each = [for r in data.azurerm_resources.vnets.resources: {r.name = split("/", split("/resourceGroups", r.id))}]
  name = each.key
  resource_group_name = each.value
}

如果这行得通,那么想法是根据当前分配的 VNet 地址 (active_vnet_addresses) 和预定义的地址确定我可以为新 VNet 提供的最低 VNet 地址空间空间 (eligible_vnet_addresses) 受限于 Azure 中每个订阅的资源组数 (980=3*255+215):

If that were to work, the idea would then be to determine the lowest possible VNet address space I can give to a new VNet based on the currently allocated VNet addresses (active_vnet_addresses) and a predefined address space (eligible_vnet_addresses) constrained by the number of resource groups (980=3*255+215) per subscription in Azure:

locals {
active_vnet_addresses = azurerm_virtual_network.vnets.address_space
eligible_vnet_addresses = concat([for s in range(1,255,1): "10.${s}.0.0/16"], [for s in range(1,255,1): "11.${s}.0.0/16"], [for s in range(1,255,1): "12.${s}.0.0/16"], [for s in range(1,215,1): "13.${s}.0.0/16"])
available_vnet_addresses = setsubtract(local.eligible_vnet_addresses, local.active_vnet_addresses)
available_vnet_numbers_sorted = sort([for a in local.available_vnet_addresses: split(".", a)[1]])
lowest_available_address_num = (length(local.available_vnet_numbers_sorted) == 0 ? "no more resource groups available" : local.available_vnet_numbers_sorted[0])
}

我对 Terraform 很陌生,这是我的最大努力,因此我非常感谢有关代码改进的建议,如果有人能指出如何在 Terraform 中从 Azure 获取已经活跃的地址空间的解决方案,我将不胜感激.

I am quite new to Terraform and this is my best effort, so I greatly appreciate suggestion on code improvements and would highly appreciate if someone could point me to a solution on how to get already active address spaces from Azure in Terraform.

推荐答案

要使您尝试的嵌套数据源正常工作,您需要像这样更改代码:

To make the nesting data sources that you tried works, you need to change your code like this:

data "azurerm_virtual_network" "vnet" {
    for_each = zipmap(flatten([for id in data.azurerm_resources.vnets.resources[*].id: element(split("/", id), 4)]), data.azurerm_resources.vnets.resources[*].name)
    resource_group_name = each.key
    name = each.value
}

这些数据源仅获取现有 VNet 详细信息的映射.地图的每个元素都是这样显示的:

These data sources only get a map of the existing VNet details. And each element of the map display like this:

"group_name" = {
    "address_space" = [
      "172.18.44.0/24",
    ]
    "guid" = "7212a68b-94a0-4be9-b972-b1c61e6ec007"
    "id" = "xxxxxxxxxxxxx"
    "location" = "southcentralus"
    "name" = "romungi-ml-studio-vnet"
    "resource_group_name" = "group_name"
    "subnets" = [
      "default",
    ]
    "vnet_peerings" = {}
  }

所以如果你只是想得到所有VNet的地址空间,你需要继续转换数据源.将详细信息转换为地图,每个成员如下所示:

So if you just want to get the address space of all the VNet, you need to continue to convert the data sources. To convert the details into a map and each member looks like this:

"VNet_name" = address_space

代码如下:

locals {
    vnet_address_spaces = zipmap(values(data.azurerm_virtual_network.vnet)[*].name, values(data.azurerm_virtual_network.vnet)[*].address_space)
}

整个地图在这里显示:

vnets = {
  "vnet1" = [
    "172.18.1.0/24",
  ]
  "vnet2" = [
    "172.17.2.0/24",
  ]
  "vnet3" = [
    "172.18.44.0/24",
  ]
  "vnet4" = [
    "10.0.0.0/16",
  ]
}

这篇关于如何在 Terraform 中获取标记的 Azure VNet 的活动地址空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆