【问题标题】:Nested loops or Cartezian product of arrays in Azure ARMAzure ARM 中的嵌套循环或数组的笛卡尔积
【发布时间】:2021-05-25 06:43:08
【问题描述】:

我正在构建一个 ARM(Azure 资源管理器)模板来创建多个相同类型的资源。假设 SQL 服务器的指标警报。我有:

  1. 3 个严重级别:[1、2、3]
  2. 20 个名称为 [sqlserver_1, sqlserver_2, ...] 的服务器
  3. 用于监控 [内存、cpu 负载、连接数] 的 3 个指标

基本上,我总共需要 180 个资源。有什么方法可以构建以及这些变量的所有可能组合。 IE。对于每台服务器,我需要监控 3 个指标,每个指标都可以根据指标级别触发 3 个可能的警报级别。

很自然地,我想到了这些数组的笛卡尔积,然后在其上进行copy 循环以填充模板属性。但是,ARM 似乎不支持此功能。

是否应该考虑编写代码生成器来创建模板而不是尝试弯曲 ARM json,而不是使用 ARM?

【问题讨论】:

    标签: arrays azure nested-loops azure-resource-manager cartesian-product


    【解决方案1】:

    关于这个问题,您可以将copy 元素添加到模板的资源部分。之后,您可以动态设置要部署的资源数量。更多详情请参考herehere

    例如

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "org": {
                "type": "array",
                "defaultValue": [
                    "contoso",
                    "fabrikam",
                    "coho"
                ]
            }
        },
        "resources": [
            {
                "apiVersion": "2017-06-01",
                "type": "Microsoft.Storage/storageAccounts",
                "name": "[concat(parameters('org')[copyIndex()], uniqueString(resourceGroup().id))]",
                "location": "[resourceGroup().location]",
                "sku": {
                    "name": "Standard_LRS"
                },
                "kind": "Storage",
                "properties": {},
                "copy": {
                    "name": "storagecopy",
                    "count": "[length(parameters('org'))]"
                }
            }
        ],
        "outputs": {}
    }
    

    【讨论】:

    • 嗯,这将是一个数组的常规副本。我正在寻找为 3 个数组中的每个值组合创建资源。
    • @Vladimir 你需要的太复杂了。它不适合手臂模板。我建议您使用 azure cli 或 powershell 脚本。实施它:docs.microsoft.com/en-us/azure/azure-monitor/alerts/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2017-08-24
    • 2018-11-09
    • 2015-06-23
    • 2015-05-14
    • 2011-08-11
    • 2017-03-05
    相关资源
    最近更新 更多