【问题标题】:Azure ARM template: expression as default value of parameter that is not string or that has allowed valuesAzure ARM 模板:表达式作为非字符串或具有允许值的参数的默认值
【发布时间】:2021-01-27 14:58:43
【问题描述】:

我正在定义一些参数来在我的 ARM 模板中配置我的 SQL 数据库弹性池。我想为每个参数指定默认值,第二个和第三个参数的默认值基于第一个参数的值。但是,每当我尝试将表达式指定为具有允许值集的参数的默认值时,我都会遇到错误:“此值不是参数'...'的允许值之一。”对于具有非字符串类型的参数也会发生同样的情况:“参数 '...' 必须是 '...' 类型。”。

我可以通过消除允许的值并将所有参数类型更改为字符串来解决此问题。但是,这将失去对用户输入值的验证。定义默认值时我做错了什么,还是这是 ARM 限制?

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sqlSkuName": {
      "type": "string",
      "allowedValues": [
        "BasicPool",
        "StandardPool",
        "PremiumPool"
      ],
      "defaultValue": "StandardPool"
    },
    "sqlSkuTier": {
      "type": "string",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ],
      "defaultValue": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 'Basic',
                        if(equals(parameters('sqlSkuName'), 'StandardPool'), 'Standard',
                        if(equals(parameters('sqlSkuName'), 'PremiumPool'), 'Premium',
                        '')))]"
    },
    "sqlDatabaseMaxCapacity": {
      "type": "int",
      "defaultValue": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 5, 50)]"
    },
  }
}

【问题讨论】:

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


    【解决方案1】:

    你必须使用对象变量来有条件地使用参数,所以你必须这样做:

    {
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sqlSkuName": {
            "type": "string",
            "allowedValues": [
                "BasicPool",
                "StandardPool",
                "PremiumPool"
            ],
            "defaultValue": "StandardPool"
        },
        "sqlSkuTier": {
            "type": "string",
            "allowedValues": [
                "Basic",
                "Standard",
                "Premium"
            ]
        },
        "sqlDatabaseMaxCapacity": {
            "type": "int",
            "defaultValue": 0
        }
    },
    "variables": {
        "sqlSkuTier-Var": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 'Basic',
                        if(equals(parameters('sqlSkuName'), 'StandardPool'), 'Standard',
                        if(equals(parameters('sqlSkuName'), 'PremiumPool'), 'Premium',
                        '')))]",
        "sqlDatabaseMaxCapacity-var": "[if(equals(parameters('sqlDatabaseMaxCapacity'),0 ),
                                            if(equals(parameters('sqlSkuName'), 'BasicPool'), 5,50),
                                            parameters('sqlDatabaseMaxCapacity'))]"
    },
    "resources": []
    

    }

    然后使用值:

    variables('sqlSkuTier-Var')
    

    我建议您使用 Visual Studio 代码插件 (https://marketplace.visualstudio.com/items?itemName=msazurermtools.azurerm-vscode-tools) 来拥有 IntelliSense,它对您的工作有很大帮助,看看您有什么问题。

    【讨论】:

    • 这会阻止我为参数指定非默认值,这对sqlDatabaseMaxCapacity 来说是个问题,因为我可能希望超出默认值 50。
    • 我想我可以使用coalesce 将可选参数与默认变量结合起来,但默认值将不再与参数相关联(例如在部署历史记录中)。
    • 您可以执行与使用 sqlSkuTier 相同的操作,我已编辑答案以接受默认值。
    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多