繁体   English   中英

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

[英]Can you do Nested Copy loops in ARM Templates?

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

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

这种帮助?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM