1

我正在尝试使用下面的代码部署多个 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",
          }
        ],

        }
    }
}
}
4

1 回答 1

1

你的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来确定长度

于 2018-08-05T15:54:53.807 回答