我有一个模块,该模块可创建具有5个子网的vNet。该模块使用块将每个子网的ID写入变量:
output "vnet_subnets" {
description = "The ids of subnets created inside the newly created vNet"
value = azurerm_subnet.subnet.*.id
}
然后我使用该块将其输出到状态文件:
output "vnet_subnets" {
value = module.virtual_network.vnet_subnets
}
在状态文件中,输出如下所示:
"vnet_subnets": {
"value": [
"/subscriptions/<my_sub_id>/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/GatewaySubnet",
"/subscriptions/<my_sub_id/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/VDMS",
"/subscriptions/<my_sub_id/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/external",
"/subscriptions/<my_sub_id/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/internal",
"/subscriptions/<my_sub_id/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/management"
],
"type": [
"tuple",
[
"string",
"string",
"string",
"string",
"string"
]
]
}
我有一个虚拟机部署,我想使用vnet状态文件作为数据源,以便可以将VM分配给正确的子网。我的问题是,如何在vnet_subnets
输出集中找到正确的子网?
答案 0 :(得分:1)
对于您的需求,我认为您遇到的问题是,您无法从输出中找到要使用的特殊字符。
您可能会使用的可能方式是,将子网ID放入带有子网名称的映射中。然后,您可以使用名称专用的子网,并从地图中获取它。这是示例代码,您可以看一下:
variable "subnets" {
type = list(string)
default = [
"test1",
"test2",
"test3"
]
}
variable "subnet_prefix" {
type = list(string)
default = [
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/24"
]
}
resource "azurerm_virtual_network" "example" {
name = "acceptanceTestVirtualNetwork1"
address_space = ["10.0.0.0/16"]
location = "East US"
resource_group_name = "group_name"
}
resource "azurerm_subnet" "example" {
count = length(var.subnets)
name = element(var.subnets, count.index)
resource_group_name = "group_name"
virtual_network_name = azurerm_virtual_network.example.name
address_prefix = element(var.subnet_prefix, count.index)
}
output "subnets_map" {
value = zipmap(var.subnets, azurerm_subnet.example.*.id)
}
我看到您使用了这些模块,因此可以将vnet模块的输出作为VM模块的输入。