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

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

问题描述

我想通过Terraform获得Terraform中具有特定标签的VNet for Azure的活动地址空间.为此,我认为我可以将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中获取活动地址空间的解决方案,我将不胜感激./p>

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天全站免登陆