如何在ARM模板中设置变量的值? Or 在 ARM 模板中使用 Or 条件

How to set the value for variable in ARM template? Or Use of Or condition in ARM template

我一直在研究 ARM 模板,我想根据用户对 skuname 的选择动态地将值分配给 isAlwayOnEnable 变量。

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
  "skuName": {
    "type": "string",
    "minLength": 1,
    "allowedValues": [
      "D1","F1","B1","B2","B3","S1","S2","S3","P1","P2","3","P1V2","P2V2","P3V2","I1","I2","I3","Y1"
    ],
    "defaultValue": "F1",
    "metadata": {
      "description": "The pricing tier for the hosting plan."
    }
  }
},
"variables": {
    "isAlwayOnEnable": "[if(equals(parameters('skuName'), or('P1','P2','P3','P1V2','P2V2','P3V2','S1','S2','S3')), 'true', 'false')]"
    },
"resources": []
}

但是在验证 arm 模板时出现问题。

{"error":{"code":"InvalidTemplate","message":"Deployment template language expression evaluation failed: 'Unable to parse language expression 'if(equals(parameters('skuName'), or('P1','P2','P3','P1V2','P2V2','P3V2','S1','S2','S3')), true, false)': expected token 'LeftParenthesis' and actual 'Comma'.'. Please see https://aka.ms/arm-template-expressions for usage details.","additionalInfo":[{"type":"TemplateViolation","info":{"lineNumber":0,"linePosition":0,"path":""}}]}}

谁能指出我哪里做错了?

我也试过用这个变量

"isAlwayOnEnable": "[or(if(equals(parameters('skuName'),'S1') ,'true','false'), if(equals(parameters('skuName'),'S2') ,'true','false'),if(equals(parameters('skuName'),'S3') ,'true','false'),  if(equals(parameters('skuName'),'P1') ,'true','false'),if(equals(parameters('skuName'),'P2') ,'true','false'),if(equals(parameters('skuName'),'P3') ,'true','false'),if(equals(parameters('skuName'),'P1V2') ,'true','false'),if(equals(parameters('skuName'),'P2V2') ,'true','false'),if(equals(parameters('skuName'),'P3V2') ,'true','false'))]"

然后我得到以下错误

{"error":{"code":"InvalidTemplate","message":"Deployment template validation failed: 'The template variable 'isAlwayOnEnable' is not valid: The provided arguments for template language function 'or' is not valid: all arguments should be of type 'boolean'. Please see https://aka.ms/arm-template-expressions#or for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.","additionalInfo":[{"type":"TemplateViolation","info":{"lineNumber":136,"linePosition":534,"path":"properties.template.variables.isAlwayOnEnable"}}]}}

变节-

"variables": {
    "isAlwayOnEnable": "[if(equals(parameters('skuName'), 'P1'), 'true','false')]"
},

skuName As P1选择否则false时,评估true。 因此,您可以使用 'OR' 运算符添加更多条件,这应该有效。

参考 - https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-logical#if

[编辑 1]

对多个值使用'OR'条件-

"variables": {
    "isAlwayOnEnable": "[or (equals(parameters('skuName'), 'P1'), equals(parameters('skuName'), 'P2'))]"
},

现在我相信你可以根据需要在逗号后添加更多equals(parameters('skuName'), 'VALUE'))

我已经在一个 ARM 模板下进行了测试,它按预期工作-

    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "skuName": {
            "defaultValue": "F1",
            "allowedValues": [
                "D1",
                "F1",
                "B1",
                "B2",
                "B3",
                "S1",
                "S2",
                "S3",
                "P1",
                "P2",
                "3",
                "P1V2",
                "P2V2",
                "P3V2",
                "I1",
                "I2",
                "I3",
                "Y1"
            ],
            "minLength": 1,
            "type": "String",
            "metadata": {
                "description": "The pricing tier for the hosting plan."
            }
        }
    },
    "variables": {
        "isAlwayOnEnable": "[or (equals(parameters('skuName'), 'P1'), equals(parameters('skuName'), 'P2'))]"
    },
    "resources": [],
    "outputs": {
        "Result": {
            "type": "bool",
            "value": "[variables('isAlwayOnEnable')]"
        }
    }
}

这符合逻辑。还有-

在将此问题标记为 helpful/not 有用之前,先在 https://portal.azure.com/#create/Microsoft.Template 上自己尝试一下。