如何使用ARM模板将NSG应用于现有子网 [英] How to apply a NSG to an existing subnet using ARM Template

查看:58
本文介绍了如何使用ARM模板将NSG应用于现有子网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ARM模板创建新的NSG,并在同一ARM模板上更新子网.我希望能够使用引用"来获取子网的地址前缀,但是这样做时,我总是会检测到循环依赖.有什么办法解决吗?我的子网臂模板部分如下所示:

I am creating new NSG with ARM template and updating the subnets at the same ARM template. I would like to be able to get subnets addressprefix with "reference" but when doing so I always get the circular dependency detected. Is there any way around it? My subnet arm template section looks like this:

      {
        "name": "[parameters('subnetName')]",
        "properties": {
          "addressPrefix": "[reference(resourceId(variables('ResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName')), '2018-03-01').addressPrefix]",
          "networkSecurityGroup": {
            "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('NSGName'))]"
          }
        }
      },

推荐答案

以下是有关如何将NSG应用于现有子网的链接:

Here is a link on how to apply a NSG to an existing subnet:

将NSG应用于现有子网

此模板使用链接模板来解决循环引用,但是您也可以使用嵌套模板在同一ARM模板中执行相同的操作(请参阅

This template uses a link template to workaround the circular reference but you can also use a nested template to do the same in the same ARM template (see Using linked and nested templates when deploying Azure resources)

这是一个使用嵌套模板执行相同操作的ARM模板:

Here is an ARM template that do the same using a nested template:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.1",
  "parameters": {
    "virtualNetworkName": {
      "type": "string",
      "metadata": {
        "description": "The name of the existing VNet"
      }
    },
    "subnetName": {
      "type": "string",
      "defaultValue": "default",
      "metadata": {
        "description": "The name of the existing subnet."
      }
    },
    "nsgName": {
      "type": "string",
      "metadata": {
        "description": "The name of the new nsg."
      }
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[parameters('nsgName')]",
      "location": "[resourceGroup().location]",
      "apiVersion": "2018-03-01",
      "properties": {

      }
    },
    {
      "apiVersion": "2017-08-01",
      "name": "apply-nsg-to-subnet",
      "type": "Microsoft.Resources/deployments",
      "resourceGroup": "[resourceGroup().name]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('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('virtualNetworkName'), '/', parameters('subnetName'))]",
              "location": "[resourceGroup().location]",
              "properties": {
                "addressPrefix": "[reference(resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName')), '2018-03-01').addressPrefix]",
                "networkSecurityGroup": {
                  "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('nsgName'))]"
                }
              }
            }
          ]
        }
      }
    }
  ],
  "outputs": {}
}

这篇关于如何使用ARM模板将NSG应用于现有子网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆