嵌套的ARM模板和dependsOn

时间:2018-10-30 16:32:27

标签: azure arm-template

我正在用我的AR​​M模板创建一组NSG规则,并尝试更新子网以在嵌套的ARM模板中使用这些NSG规则。模板部署失败,并显示“正在进行对该资源或从属资源的另一操作”。我试图在嵌套模板中使用“ dependsOn”功能,但这不能解决问题。我试图给出NSG名称和resourceId()

"[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]",

没有运气的依靠。在尝试更新子网之前,是否有更好的方法等待NSG规则准备就绪?

模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "virtualNetName": {
      "type": "string",
    },
    "subnetName": {
      "type": "string",
    }
  },
  "variables": {
    "NSGName": parameters('subnetName')
    "ResourceGroupName": "[resourceGroup().name]"
  },
  "resources": [
    {
      "apiVersion": "2017-11-01",
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[variables('NSGName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "securityRules": [
          {
            "name": "Allow-Inbound-RDP",
            "properties": {
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "3389",
              "sourceAddressPrefix": "192.168.0.1/24",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 4050,
              "direction": "Inbound"
            }
          }
        ]
      }
    },
    {
      "apiVersion": "2017-08-01",
      "name": "apply-nsg-to-subnet",
      "type": "Microsoft.Resources/deployments",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]"
      ],
      "properties": {
        "mode" : "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "apiVersion" : "2018-03-01",
              "type": "Microsoft.Network/virtualNetworks/subnets",
              "name": "[concat(parameters('virtualNetName'), '/', parameters('subnetName'))]",
              "properties": {
                "addressPrefix": "[reference(resourceId(variables('ResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetName'), parameters('subnetName')), '2018-03-01').addressPrefix]",
                "networkSecurityGroup": {
                  "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]"
                }
              }
            }
          ]
        }
      }
    }
  ]
}

我相信一个NSG和一个子网更新会顺利进行,但是当我使用八个NSG和一个子网进行更新时不会。

1 个答案:

答案 0 :(得分:1)

您的嵌套部署本身需要依赖于[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]。在部署内部,您不能依赖于部署外​​部的任何内容(并且由于其嵌套在父对象中的所有内容都在其外部)。