将 NSG 应用到子网时,不需要 Azure ARM 模板 'copyIndex'

标签 azure azure-rm-template

我正在使用复制功能创建 NSG,它工作正常。但是,我想以类似的方式将 NSG 应用于子网,但我得到的复制索引不是预期的。

{
  "apiVersion": "2017-08-01",
  "name": "apply-nsg-to-subnet",
  "type": "Microsoft.Resources/deployments",
  "properties": {
    "mode": "Incremental",
    "template": {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "#{BuildNumber}#",
      "resources": [
        {
          "apiVersion": "2018-06-01",
          "type": "Microsoft.Network/virtualNetworks/subnets",
          "name": "[concat(parameters('vnetName') , '/' , parameters('subnets').settings[copyIndex()].name)]",
          "location": "[variables('location')]",
          "copy":{
            "name": "subnetLoop",
            "count": "[variables('subnetcount')]",
            "mode": "Serial"
          },
          "properties": {
            "addressPrefix": "[parameters('subnets').settings[copyIndex()].addressPrefix]",
            "networkSecurityGroup": {
              "id": "[resourceId('Microsoft.Network/networkSecurityGroups', concat(parameters('nsgNameAffix'), parameters('subnets').settings[copyIndex()].name, variables('nsgNameSuffix')))]"
            }
          }
        }
      ]
    }
  }
}

我的 copyIndex 使用有什么问题以及在这种情况下应该如何使用它?

最佳答案

这是由于您正在使用的嵌套内联模板,我能够重现并且能够使用此示例模板来解决:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "subnets": {
            "type": "array",
            "defaultValue": [
                {
                    "name": "testo",
                    "addressPrefix": "10.0.0.0/24"
                },
                {
                    "name": "testo1",
                    "addressPrefix": "10.0.1.0/24"
                }
            ]
        }
    },
    "resources": [
        {
            "apiVersion": "2018-08-01",
            "name": "vnet-testo",
            "type": "Microsoft.Network/virtualNetworks",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                }
            }
        },
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[parameters('subnets')[copyIndex()].name]",
            "location": "[resourceGroup().location]",
            "copy": {
                "name": "nsg",
                "count": "[length(parameters('subnets'))]"
            },
            "properties": {
                "securityRules": []
            }
        },
        {
            "name": "NestedDeployment1",
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2015-01-01",
            "dependsOn": [
                "nsg",
                "vnet-testo"
            ],
            "properties": {
                "mode": "Incremental",
                "templateLink": {
                    "uri": "https://paste.ee/d/iCWEu/0",
                    "contentVersion": "1.0.0.0"
                },
                "parameters": {
                    "subnets": {
                        "value": "[parameters('subnets')]"
                    }
                }
            }
        }
    ]
}

基本上,我刚刚将您的模板转换为嵌套模板(不是内联)。

ps。查看我的副本定义,它比你的好一点。

关于将 NSG 应用到子网时,不需要 Azure ARM 模板 'copyIndex',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54315279/

相关文章:

c# - 绑定(bind)到 CosmosDB 输入时需要“Id”

azure - 使用 ARM 模板从私有(private) Github 存储库部署网站

azure - 如何使用 azure powershell 命令获取给定存储帐户名称和资源组的存储帐户 ID?

azure - 如何使用 Azure ARM 模板将 VM 部署到资源组 "A"并引用资源组 "B"中的现有 key 保管库?

json - 使用 ARM 模板创建 NIC 时如何引用另一个虚拟网络/资源组的子网

azure - 如何使用 ARM 模板为 Azure 资源创建事件日志诊断设置

azure - 我应该何时重新打包我的 Azure 计算角色?

Azure DNS - 公共(public) Internet 未找到域

azure - Azure 资源组中的部署最大限制

azure - 如何在变量中使用 Azure Arm 模板输出?