如何在Azure资源管理器模板中使用“锯齿状”对象进行迭代?

时间:2019-01-21 14:12:32

标签: azure-resource-manager

我的对象具有不等数量的属性(并且希望保持这种状态),即第二个对象缺少属性“ routeTable”,而属性重复即“ NSG-AllowAll”

"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"
                },
                {
                    "name": "routed",
                    "addressPrefix": "10.2.2.0/24",
                    "routeTable": "UDR-Default1"
                }
            ]

用于创建vnet的solution可以正常工作,但前提是现有属性不重复,即上面的第二个NSG与第一个名称不同。在我的情况下,会有很多重复的属性名称

1 个答案:

答案 0 :(得分:1)

在这种情况下,您只需要从我创建的模板中删除nsg \ udr。我想那也是我在那儿告诉过你的。检查my previous answer中的ps

{
    "$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-AllowAll"
                },
                {
                    "name": "routed",
                    "addressPrefix": "10.2.2.0/24",
                    "routeTable": "UDR-Default"
                }
            ]
        }
    },
    "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": [
        {
            "apiVersion": "2017-06-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[concat(parameters('deploymentPrefix'), '-vNet')]",
            "location": "[resourceGroup().location]",
            "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')]))]"
                        }
                    }
                ]
            }
        }
    ]
}

如果您有nsg \ udr,我看不到任何原因为何无法解决