Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure ARM模板&x27;copyIndex';将NSG应用于子网时不需要_Azure_Arm Template_Azure Template - Fatal编程技术网

Azure ARM模板&x27;copyIndex';将NSG应用于子网时不需要

Azure ARM模板&x27;copyIndex';将NSG应用于子网时不需要,azure,arm-template,azure-template,Azure,Arm Template,Azure Template,我正在创建具有复制功能的NSG,它工作正常。但是,我希望以类似的方式将NSG应用于子网,但我得到的copyindex不是预期的 { "apiVersion": "2017-08-01", "name": "apply-nsg-to-subnet", "type": "Microsoft.Resources/deployments", "properties": { "mode": "Incremental", "template": { "$schem

我正在创建具有复制功能的NSG,它工作正常。但是,我希望以类似的方式将NSG应用于子网,但我得到的copyindex不是预期的

{
  "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')]"
                    }
                }
            }
        }
    ]
}
基本上,我刚刚将您的模板转换为嵌套模板(不是内联模板)


另外,请检查我的副本定义,它比您的好一点。

您能显示完整的错误文本吗?“消息”:“无法处理资源“/subscriptions/***********/Microsoft.Resources/deployments/apply nsg to subnet”在第128行和第9列的模板语言表达式。”此位置不需要模板函数“copyIndex”。该函数只能在指定了副本的资源中使用。有关用法的详细信息,请参阅。“”就是这样。这是否意味着我不能将copyloop与嵌套模板一起使用?如果可能,我宁愿将模板保持内联。联机模板有很多怪癖。我宁愿不使用它们(我不这样做)。您可以尝试将副本移动到模板而不是资源。这会起作用(我认为)我在更高的级别上移动了副本,但它给出了错误消息“在模板中多次定义”“如果是在较低级别,我会得到与原始问题类似的错误。如果将copyindex移动到较高级别,则还需要动态构造部署名称。”。因为每个副本都是一个单独的部署,所以它们的名称必须不同。现在我让它工作了。它很慢,但很有效。再次感谢!