您可以在ARM模板中执行嵌套复制循环吗?

问题描述

我正在尝试动态生成路径映射,以将传入流量路由到App Gateway的正确后端池。

例如,我们有20个租户,每个后端池允许5个租户,这意味着我们将生成4个后端池。

我需要动态创建路径映射,以便后端池一为租户1-5服务,后端池二为租户6-10服务,等等。

我想要生成的所需数组是:

[
    [ "tenant1","tenant2","tenant3","tenant4","tenant5"],["tenant6","tenant7","tenant8","tenant9","tenant10"],["tenant11","tenant12","tenant13","tenant14","tenant15"],["tenant16","tenant17","tenant18","tenant19","tenant20"]
]

形成此数组后,我可以创建后端池,并连接子数组字符串以形成所需的路径映射。

这是其中一种尝试的快速原型...

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
  },"variables": {
    "totalTenants": 20,"tenantsPerBackendPool": 5,"copy": [
      {
        "name": "outercopy","count": "[div(variables('totalTenants'),variables('tenantsPerBackendPool'))]","input": {
          "copy": {
            "count": "[variables('totalTenants')]","name": "innercopy","input": "[if(equals(div(copyIndex('innercopy'),5),copyIndex('outercopy')),concat('/tenant',copyIndex('innercopy'),'/*'),json('null'))]"
          }
        }
      }
    ]
  },"resources": [
    // Multiple backend pools will be created here,and use the path mappings to route correctly
  ],"outputs": {
    "pathMappings": {
      "type": "array","value": "[variables('outercopy')]"
    }
  }
}

但是我遇到以下异常: New-AzResourceGroupDeployment: 16:01:18 - Error: Code=InvalidTemplate; Message=Deployment template language expression evaluation Failed: 'The template language function 'copyIndex' has an invalid argument. The provided copy name 'innercopy' doesn't exist in the resource.

解决方法

我很确定您不能在OP中执行嵌套方法,但是我认为您可以生成所需的数组数组:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {},"variables": {
        "totalTenants": 20,"tenantsPerBackendPool": 5,"copy": [
            {
                "name": "firstPass","count": "[variables('totalTenants')]","input": "[concat('/tenant',copyIndex('firstPass',1),'/*')]"
            },{
                "name": "finalpass","count": "[div(variables('totalTenants'),variables('tenantsPerBackendPool'))]","input": "[take(skip(variables('firstPass'),mul(variables('tenantsPerBackendPool'),copyIndex('finalPass'))),variables('tenantsPerBackendPool'))]"
            }
        ]
    },"resources": [ ],"outputs": {
        "firstPass": {
            "type": "array","value": "[variables('firstPass')]"
        },"finalPass": {
            "type": "array","value": "[variables('finalpass')]"
        }
    }
}

有帮助吗?