【问题标题】:Can you do Nested Copy loops in ARM Templates?您可以在 ARM 模板中执行嵌套复制循环吗?
【发布时间】:2020-10-06 15:28:57
【问题描述】:

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

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

我需要动态创建路径映射,以便后端池 1 服务于租户 1-5,后端池 2 服务于租户 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.

【问题讨论】:

  • 我现在已经硬编码了我的预期输出!

标签: azure azure-resource-manager arm-template azure-template


【解决方案1】:

我很确定你不能在 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')]"
        }
    }
}

有帮助吗?

【讨论】:

  • 跳过/学习 ARM 模板,我现在都看过了!谢谢摩尔先生。
  • 这里是上述技术的资源:@​​987654321@
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2017-03-11
  • 2015-06-05
  • 2013-08-09
  • 2013-08-18
  • 2012-10-25
  • 2020-09-15
相关资源
最近更新 更多