迭代不存在的 Azure 资源管理器 (ARM) 对象属性?
Iterate over not existing Azure resource manager (ARM) object properties?
我有一个对象的属性数量不相等(并希望保持这样),即第二个对象丢失 属性 "routeTable"
"subnets": {
"value":[
{
"name": "GatewaySubnet",
"addressPrefix": "10.2.0.0/24",
"networkSecurityGroup":"NSG-AllowAll",
"routeTable":"UDR-Default"
},
{
"name":"UnTrusted",
"addressPrefix":"10.2.1.0/24",
"networkSecurityGroup":"NSG-AllowAll",
}]}
现在我不知道如何在遍历对象时检查 属性 是否存在。由于缺少 属性 "id",下面给出了错误:“[resourceID('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnets') ].routeTable)]"
我的嵌套条件 "id" 属性 似乎不起作用,即
"networkSecurityGroup": {
"id": "[resourceID('Microsoft.Network/networkSecurityGroups', if(equals(parameters('subnets')[copyIndex('subnets')].networkSecurityGroup, ''), json('null'), parameters('subnets')[copyIndex('subnets')].networkSecurityGroup))]"
}
好的,这是我能想到的最好的:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentPrefix": {
"type": "string"
},
"subnets": {
"type": "array",
"defaultValue": [
{
"name": "GatewaySubnet",
"addressPrefix": "10.2.0.0/24",
"networkSecurityGroup": "NSG-AllowAll",
"routeTable": "UDR-Default"
},
{
"name": "UnTrusted",
"addressPrefix": "10.2.1.0/24",
"networkSecurityGroup": "NSG-AllowAll1"
},
{
"name": "routed",
"addressPrefix": "10.2.2.0/24",
"routeTable": "UDR-Default1"
}
]
}
},
"variables": {
"copy": [
{
"name": "subnetsBase",
"count": "[length(parameters('subnets'))]",
"input": {
"name": "[concat('subnet-', parameters('subnets')[copyIndex('subnetsBase')].name)]",
"properties": {
"addressPrefix": "[parameters('subnets')[copyIndex('subnetsBase')].addressPrefix]"
}
}
},
{
"name": "subnetsUDR",
"count": "[length(parameters('subnets'))]",
"input": {
"routeTable": {
"id": "[if(contains(parameters('subnets')[copyIndex('subnetsUDR')], 'routeTable'), resourceId('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnetsUDR')].routeTable), 'skip')]"
}
}
},
{
"name": "subnetsNSG",
"count": "[length(parameters('subnets'))]",
"input": {
"networkSecurityGroup": {
"id": "[if(contains(parameters('subnets')[copyIndex('subnetsNSG')], 'networkSecurityGroup'), resourceId('Microsoft.Network/networkSecurityGroups', parameters('subnets')[copyIndex('subnetsNSG')].networkSecurityGroup), 'skip')]"
}
}
}
]
},
"resources": [
{
"condition": "[not(contains(variables('subnetsNSG')[copyIndex()].networkSecurityGroup.id, 'skip'))]",
"apiVersion": "2017-06-01",
"name": "[if(contains(parameters('subnets')[copyIndex()], 'networkSecurityGroup'), parameters('subnets')[copyIndex()].networkSecurityGroup, 'skip')]",
"location": "[resourceGroup().location]",
"type": "Microsoft.Network/networkSecurityGroups",
"copy": {
"name": "nsg",
"count": "[length(parameters('subnets'))]"
},
"properties": {
"securityRules": []
}
},
{
"condition": "[not(contains(variables('subnetsUDR')[copyIndex()].routeTable.id, 'skip'))]",
"type": "Microsoft.Network/routeTables",
"name": "[if(contains(parameters('subnets')[copyIndex()], 'routeTable'), parameters('subnets')[copyIndex()].routeTable, 'skip')]",
"apiVersion": "2017-10-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "udr",
"count": "[length(parameters('subnets'))]"
},
"properties": {
"routes": []
}
},
{
"apiVersion": "2017-06-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "[concat(parameters('deploymentPrefix'), '-vNet')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"nsg",
"udr"
],
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.2.0.0/16"
]
},
"copy": [
{
"name": "subnets",
"count": "[length(parameters('subnets'))]",
"input": {
"name": "[concat('subnet-', parameters('subnets')[copyIndex('subnets')].name)]",
"properties": "[union(variables('subnetsBase')[copyIndex('subnets')].properties, if(equals(variables('subnetsUDR')[copyIndex('subnets')].routetable.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsUDR')[copyIndex('subnets')]), if(equals(variables('subnetsNSG')[copyIndex('subnets')].networkSecurityGroup.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsNSG')[copyIndex('subnets')]))]"
}
}
]
}
}
]
}
您可能可以使用一些嵌套循环使其变得更好。但这也有效。
PS。我为 nsg\udr 使用了不同的名称,因为我正在动态创建它们,在您的场景中,如果这些名称存在,它将使用相同的名称(这不会)。
我有一个对象的属性数量不相等(并希望保持这样),即第二个对象丢失 属性 "routeTable"
"subnets": {
"value":[
{
"name": "GatewaySubnet",
"addressPrefix": "10.2.0.0/24",
"networkSecurityGroup":"NSG-AllowAll",
"routeTable":"UDR-Default"
},
{
"name":"UnTrusted",
"addressPrefix":"10.2.1.0/24",
"networkSecurityGroup":"NSG-AllowAll",
}]}
现在我不知道如何在遍历对象时检查 属性 是否存在。由于缺少 属性 "id",下面给出了错误:“[resourceID('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnets') ].routeTable)]"
我的嵌套条件 "id" 属性 似乎不起作用,即
"networkSecurityGroup": {
"id": "[resourceID('Microsoft.Network/networkSecurityGroups', if(equals(parameters('subnets')[copyIndex('subnets')].networkSecurityGroup, ''), json('null'), parameters('subnets')[copyIndex('subnets')].networkSecurityGroup))]"
}
好的,这是我能想到的最好的:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"deploymentPrefix": {
"type": "string"
},
"subnets": {
"type": "array",
"defaultValue": [
{
"name": "GatewaySubnet",
"addressPrefix": "10.2.0.0/24",
"networkSecurityGroup": "NSG-AllowAll",
"routeTable": "UDR-Default"
},
{
"name": "UnTrusted",
"addressPrefix": "10.2.1.0/24",
"networkSecurityGroup": "NSG-AllowAll1"
},
{
"name": "routed",
"addressPrefix": "10.2.2.0/24",
"routeTable": "UDR-Default1"
}
]
}
},
"variables": {
"copy": [
{
"name": "subnetsBase",
"count": "[length(parameters('subnets'))]",
"input": {
"name": "[concat('subnet-', parameters('subnets')[copyIndex('subnetsBase')].name)]",
"properties": {
"addressPrefix": "[parameters('subnets')[copyIndex('subnetsBase')].addressPrefix]"
}
}
},
{
"name": "subnetsUDR",
"count": "[length(parameters('subnets'))]",
"input": {
"routeTable": {
"id": "[if(contains(parameters('subnets')[copyIndex('subnetsUDR')], 'routeTable'), resourceId('Microsoft.Network/routeTables', parameters('subnets')[copyIndex('subnetsUDR')].routeTable), 'skip')]"
}
}
},
{
"name": "subnetsNSG",
"count": "[length(parameters('subnets'))]",
"input": {
"networkSecurityGroup": {
"id": "[if(contains(parameters('subnets')[copyIndex('subnetsNSG')], 'networkSecurityGroup'), resourceId('Microsoft.Network/networkSecurityGroups', parameters('subnets')[copyIndex('subnetsNSG')].networkSecurityGroup), 'skip')]"
}
}
}
]
},
"resources": [
{
"condition": "[not(contains(variables('subnetsNSG')[copyIndex()].networkSecurityGroup.id, 'skip'))]",
"apiVersion": "2017-06-01",
"name": "[if(contains(parameters('subnets')[copyIndex()], 'networkSecurityGroup'), parameters('subnets')[copyIndex()].networkSecurityGroup, 'skip')]",
"location": "[resourceGroup().location]",
"type": "Microsoft.Network/networkSecurityGroups",
"copy": {
"name": "nsg",
"count": "[length(parameters('subnets'))]"
},
"properties": {
"securityRules": []
}
},
{
"condition": "[not(contains(variables('subnetsUDR')[copyIndex()].routeTable.id, 'skip'))]",
"type": "Microsoft.Network/routeTables",
"name": "[if(contains(parameters('subnets')[copyIndex()], 'routeTable'), parameters('subnets')[copyIndex()].routeTable, 'skip')]",
"apiVersion": "2017-10-01",
"location": "[resourceGroup().location]",
"copy": {
"name": "udr",
"count": "[length(parameters('subnets'))]"
},
"properties": {
"routes": []
}
},
{
"apiVersion": "2017-06-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "[concat(parameters('deploymentPrefix'), '-vNet')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"nsg",
"udr"
],
"properties": {
"addressSpace": {
"addressPrefixes": [
"10.2.0.0/16"
]
},
"copy": [
{
"name": "subnets",
"count": "[length(parameters('subnets'))]",
"input": {
"name": "[concat('subnet-', parameters('subnets')[copyIndex('subnets')].name)]",
"properties": "[union(variables('subnetsBase')[copyIndex('subnets')].properties, if(equals(variables('subnetsUDR')[copyIndex('subnets')].routetable.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsUDR')[copyIndex('subnets')]), if(equals(variables('subnetsNSG')[copyIndex('subnets')].networkSecurityGroup.id, 'skip'), variables('subnetsBase')[copyIndex('subnets')].properties, variables('subnetsNSG')[copyIndex('subnets')]))]"
}
}
]
}
}
]
}
您可能可以使用一些嵌套循环使其变得更好。但这也有效。
PS。我为 nsg\udr 使用了不同的名称,因为我正在动态创建它们,在您的场景中,如果这些名称存在,它将使用相同的名称(这不会)。