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



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

例如,我们有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')]"
}
}
}

那个帮助?

最新更新