首页 > 解决方案 > 是否有一种解决方法可以让 Azure ARM 模板复制块接受零长度数组?

问题描述

我正在开发一个 Azure ARM 模板,该模板将有一个字符串参数,需要一个以逗号分隔的电子邮件地址列表。在模板中,我想解析它并复制到资源类型emailReceivers属性所需的对象类型数组中。Microsoft.Insights/ActionGroups

输入必须是单个字符串,因为该值将被 Octopus Deploy 替换为我们部署管道的一部分。

只要提供至少一个电子邮件地址,我的模板就可以正常工作,但我希望这个值是可选的。不幸的是,当提供空字符串时,我收到以下错误:

'0' 行和 '0' 列的模板 'copy' 定义具有无效的副本计数。复制计数必须是正整数值并且不能超过“800”。

显然,这些复制块不支持零长度数组,所以我想知道是否有人知道一种解决方法或狡猾的黑客可以让我实现我想要的。

这是一个精简的模板示例:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "emailAddresses": {
      "type": "string",
      "defaultValue": "one@email.com, two@email.com",
      "metadata": {
        "description": "Comma-separated list of email recipients."
      }
    }
  },
  "variables": {
    "emailArray": "[if(equals(length(parameters('emailAddresses')), 0), json('[]'), split(parameters('emailAddresses'),','))]",
    "copy": [
      {
        "name": "emailReceivers",
        "count": "[length(variables('emailArray'))]",
        "input": {
            "name": "[concat('Email ', trim(variables('emailArray')[copyIndex('emailReceivers')]))]",
            "emailAddress": "[trim(variables('emailArray')[copyIndex('emailReceivers')])]"
        }
      }
    ]
  },
  "resources": [],
  "outputs": {
      "return": {
          "type": "array",
          "value": "[variables('emailReceivers')]"
      }
  }
}

标签: azureazure-resource-manager

解决方案


好吧,不是直接的,但你可以这样做:

"copy": [
  {
    "name": "emailReceivers",
    "count": "[if(equals(length(variables('emailArray')), 0), 1, length(variables('emailArray')))]",
    "input": {
        "name": "[concat('Email ', trim(variables('emailArray')[copyIndex('emailReceivers')]))]",
        "emailAddress": "[trim(variables('emailArray')[copyIndex('emailReceivers')])]" << these need the same if to put placeholder value inside
    }
  }
]

然后,如果长度等于 0 bla-bla-bla,您将在某处实现条件


推荐阅读