phy*_*man 3 azure azure-resource-manager azure-bicep
我试图在创建具有 4 个子网的虚拟网络的模块中输出每个子网的引用 ID。我可以得到第一个,[0]但是当我尝试输出其他的时,[1], [2], [3]部署失败并抛出错误:
The language expression property array index "1" is out of bounds
以下是创建 virtualNetwork 和子网的代码:
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2018-11-01' = {
name: vNetName
location: location
tags: tags
properties: {
addressSpace: {
addressPrefixes: [
addressPrefix
]
}
subnets: subnets
}
}
Run Code Online (Sandbox Code Playgroud)
subnets是数组类型的变量:
var subnets = [
{
name: mgmtSubnetName
properties: {
addressPrefix: mgmtSubnetAddressPrefix
}
}
{
name: intSubnetName
properties: {
addressPrefix: intSubnetAddressPrefix
}
}
{
name: extSubnetName
properties: {
addressPrefix: extSubnetAddressPrefix
}
}
{
name: vdmsSubnetName
properties: {
addressPrefix: vdmsSubnetAddressPrefix
}
}
]
Run Code Online (Sandbox Code Playgroud)
当我使用下面的输出行时,它返回一个包含 4 个对象的数组...每个对象对应一个创建的子网:
output subnets array = virtualNetwork.properties.subnets
每个对象具有以下格式:
{
"name":"<value>",
"id":"<value>",
"etag":"<value>",
"properties":{
"provisioningState":"Succeeded",
"addressPrefix":"<value>",
"ipConfigurations":[
{
"id":"<value>"
}
],
"delegations":[]
},
"type":"Microsoft.Network/virtualNetworks/subnets"
}
Run Code Online (Sandbox Code Playgroud)
当我使用下面的输出行时,它返回子网数组中的第一个对象:
output subnet1 object = virtualNetwork.properties.subnets[0]
当我使用下面的输出行时,它返回resourceId第一个子网的:
output subnet1 string = virtualNetwork.properties.subnets[0].id
我无法使用索引1, 2,或检索数组中的其他对象3。
我也尝试过该函数(下面的示例),但索引或resourceId的行为完全相同:1, 2,3
output subnet1Id string = resourceId('Microsoft.Network/VirtualNetworks/subnets', name, subnets[0].name)
您可以使用下面的 bicep 模板来部署 vnet 和子网,并输出子网和子网 ID,如下所示:
var subnets = [
{
name: 'vm-subnet'
properties: {
addressPrefix:'10.0.0.0/24'
}
}
{
name: 'webapp-subnet'
properties: {
addressPrefix:'10.0.1.0/24'
}
}
{
name: 'appgw-subnet'
properties: {
addressPrefix:'10.0.2.0/24'
}
}
{
name: 'bastion-subnet'
properties: {
addressPrefix:'10.0.3.0/24'
}
}
]
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2018-11-01' = {
name: 'ansuman-vnet'
location: 'east us'
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
subnets: subnets
}
}
output subnets array = [for (name, i) in subnets :{
subnets : virtualNetwork.properties.subnets[i]
}]
output subnetids array = [for (name, i) in subnets :{
subnets : virtualNetwork.properties.subnets[i].id
}]
output subnetappgw string = virtualNetwork.properties.subnets[2].id
output webappsubnet object = virtualNetwork.properties.subnets[1]
Run Code Online (Sandbox Code Playgroud)
输出:
注意:我使用的是最新的 Bicep 版本 ieBicep CLI version 0.4.1124
| 归档时间: |
|
| 查看次数: |
7037 次 |
| 最近记录: |