我可以有条件地使用ARM模板中的复制功能吗?

时间:2019-06-18 15:16:12

标签: azure azure-web-sites azure-resource-manager arm-template

我们的解决方案已部署到开发,测试和生产多个环境中。我有条件地为非开发环境部署虚拟网络和其他强大的网络基础架构。我一直在努力将访问限制应用于App Service的Web配置,仅当布尔值为true(使用copyIndex)时。

以下用于将子网访问限制分配给App Service:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "ukwest"
    }
  },
  "variables": {
    "networkingRequired": true,
    "aspName": "xxxMyAppServicePlan",
    "siteName": "xxxMySite1",
    "vnetName": "superVnetName",
    "subnetNames": [
      "subnetone",
      "subnettwo",
      "subnetthree"
    ]
  },
  "resources": [
    {
      "name": "[variables('aspName')]",
      "type": "Microsoft.Web/serverfarms",
      "kind": "app",
      "apiVersion": "2018-02-01",
      "location": "[parameters('location')]",
      "properties": {},
      "sku": {
        "name": "S1",
        "capacity": 1
      }
    },
    {
      "kind": "app",
      "name": "[variables('siteName')]",
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-02-01",
      "location": "[parameters('location')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('aspName'))]",
        "siteConfig": {
          "clientAffinityEnabled": false,
          "httpsOnly": true,
          "alwaysOn": true,
          "virtualApplications": [
            {
              "virtualPath": "/",
              "physicalPath": "site\\wwwroot",
              "preloadEnabled": true
            }
          ],
          "copy": [
            {
              "name": "ipSecurityRestrictions",
              "count": "[length(variables('subnetNames'))]",
              "input": {
                "vnetSubnetResourceId": "[resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), variables('subnetNames')[copyIndex('ipSecurityRestrictions')])]",
                "action": "Allow",
                "priority": "1",
                "name": "[variables('subnetNames')[copyIndex('ipSecurityRestrictions')]]",
                "description": "[concat(variables('subnetNames')[copyIndex('ipSecurityRestrictions')], ' subnet')]"
              }
            }
          ]
        }
      },
      "dependsOn": [
        "[variables('aspName')]"
      ]
    }
  ]
}

所以我现在需要做的是让它尊重变量“ networkingRequired”,并且仅在网络为真时才对ipSecurityRestrictions做“复制”。

1 个答案:

答案 0 :(得分:1)

最简单的方法-将副本移至变量部分,并使用表达式“即时”定义ipSecurityRestrictions的值。

"variables": {
    "empty": [],
    "copy": [you copy goes here]
},
...
"ipSecurityRestrictions": "[if(variables('networkingRequired'), variables('ipSecurityRestrictions'), variables('empty'))]"