Azure ARM 副本索引在 Json 中不起作用

Azure ARM copy Index not working in Json

我正在尝试使用下面的代码部署多个 Azure VNET,它

它给出以下错误

{
  "error": {
    "code": "InvalidTemplate",
    "details": null,
    "message": "Deployment template validation failed: 'The template resource '[concat(variables('namePrefix'), parameters('VNetSettings').vnets.name, [copyIndex(1)])]' at line '1' and column '923' is not valid: The language expression property 'name' has an invalid array index.. Please see https://aka.ms/arm-template-expressions for usage details.'.",
    "target": null
  },
  "properties": null
}

下面是我的代码。我想使用 coyIndex 遍历多个 azure 虚拟网络实例。根据提供的 vnet 名称数量,我希望它获得要创建的实例数量。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "VNetSettings": {
            "type": "object"
        },
        "namingSettings": {
            "type": "object"
        }
    },
    "variables": {
        "namePrefix": "[concat(parameters('namingSettings').name.org,'-',parameters('namingSettings').name.accountEnv,'-',parameters('namingSettings').name.sdlcEnv,'-',parameters('namingSettings').name.region,'-',parameters('namingSettings').name.appname,'-')]"
    },
    "resources": [
        {
            "apiVersion": "2018-04-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[concat(variables('namePrefix'), parameters('VNetSettings').vnets.name, [copyIndex(1)])]",
            "location": "[resourceGroup().location]",
            "copy": {
                "name": "vnetcopy",
                "count": "[length(parameters('VNetSettings').vnets.name)]"
            },
            "scale": null,
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('VNetSettings').vnets.cidr]"
                    ]
                }
            }
        }
    ]
}

更新

参数文件enter code here

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",

  "parameters":{
     "namingSettings": {
      "value": {
        "name": {
          "org": "it",
          "accountEnv": "nonprod",
          "sdlcEnv": "it-test",
          "region": "eastus2",
          "tier": "",
          "object": "",
          "networkbuild": "network",
          "auditbuild": "audit",
          "automationbuild": "automation",
          "dnsbuild": "dns",
          "appname": "network"
        }
      }
    },
     "VNetSettings": {
      "value": {
        "vnets": [
          {
            "name": "vnet0",
            "cidr": "10.10.10.0/24",
          }
        ],

        }
    }
}
}

您的concat()输入有误。 copyIndex() 不应在 [] 内。试试这个:

"[concat(variables('namePrefix'), parameters('VNetSettings').vnets.name, copyIndex(1))]"

copyIndex()只是另一个函数,无论里面有多少个函数,你只用[]包裹一次字符串

您需要调整您的模板以反映您正在遍历数组的事实(现在您遍历名称,vnets.name 在 ARM 模板中不是有效的结构)。

你需要做这样的事情:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "VNetSettings": {
            "type": "object"
        },
        "namingSettings": {
            "type": "object"
        }
    },
    "variables": {
        "namePrefix": "[concat(parameters('namingSettings').name.org,'-',parameters('namingSettings').name.accountEnv,'-',parameters('namingSettings').name.sdlcEnv,'-',parameters('namingSettings').name.region,'-',parameters('namingSettings').name.appname,'-')]"
    },
    "resources": [
        {
            "apiVersion": "2018-04-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[concat(variables('namePrefix'), parameters('VNetSettings').vnets[copyIndex()].name)]",
            "location": "[resourceGroup().location]",
            "copy": {
                "name": "vnetcopy",
                "count": "[length(parameters('VNetSettings').vnets)]"
            },
            "scale": null,
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('VNetSettings').vnets[copyIndex()].cidr]"
                    ]
                }
            }
        }
    ]
}

请注意,您需要使用 copyIndex() 来获取数组中的当前 vnet,并且您需要使用 .vnet 来确定长度