使用ARM模板

时间:2019-08-30 09:47:04

标签: azure arm-template azure-virtual-network

使用ARM模板部署子网资源时,我试图有条件地部署路由模板,但是,如果使用if条件,则无法这样做。有人会知道正确的语法还是我做错了什么?

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "routeTableConfigurationObject": {
        "type": "object",
        "defaultValue": null
    },
    "vnetConfigurationObject": {
        "type": "object"
    },
    "Tags": {
        "type": "object"
    }
},
"resources": [
    {
        "type": "Microsoft.Network/virtualNetworks/Subnets",
        "name": "[concat(parameters('vnetConfigurationObject').name,'/',parameters('vnetConfigurationObject').subnets[copyIndex()].name)]",
        "location": "[parameters('vnetConfigurationObject').location]",
        "apiVersion": "2018-08-01",
        "copy": {
            "name": "vnet",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "mode": "serial"
        },
        "properties": {
            "addressPrefix": "[parameters('vnetConfigurationObject').subnets[copyIndex()].addressPrefix]",
            "routeTable": "[if(empty('routeTableConfigurationObject'), json('null'), json(concat('{\"id\": \"', '/subscriptions/', parameters('routeTableConfigurationObject').subscriptionId, '/resourceGroups/', parameters('routeTableConfigurationObject').resourceGroupName, '/providers/Microsoft.Network/routeTables/', parameters('vnetConfigurationObject').subnets[copyIndex()].routeTable,'\"}')))]"
        }
    }
]

}

1 个答案:

答案 0 :(得分:0)

尝试一下:

"variables": [
    "copy": [
        {
            "name": "route",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "input": {
                "id": "[resourceId(parameters('routeTableConfigurationObject').subscriptionId, parameters('routeTableConfigurationObject').resourceGroupName, 'Microsoft.Network/routeTables', parameters('vnetConfigurationObject').subnets[copyIndex('route')].routeTable)]"
            }
        }
    ]
],
"resources": [
    {
        "type": "Microsoft.Network/virtualNetworks/Subnets",
        "name": "[concat(parameters('vnetConfigurationObject').name,'/',parameters('vnetConfigurationObject').subnets[copyIndex()].name)]",
        "location": "[parameters('vnetConfigurationObject').location]",
        "apiVersion": "2018-08-01",
        "copy": {
            "name": "vnet",
            "count": "[length(parameters('vnetConfigurationObject').subnets)]",
            "mode": "serial"
        },
        "properties": {
            "addressPrefix": "[parameters('vnetConfigurationObject').subnets[copyIndex()].addressPrefix]",
            "routeTable": "[if(empty('routeTableConfigurationObject'), json('null'), variables('route')[copyIndex()])]"
        }
    }
]

我没有时间\能力来测试它,但是应该很接近