Search code examples
azureazure-devopsazure-resource-managerazure-rm-templateazure-deployment

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


I been working on ARM template and i want asign value to isAlwayOnEnable variable dynamically based on user selection of skuname.

{
"$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": []
}

But it has an issue while validating the arm template.

{"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":""}}]}}

Can anyone point me where i am doing the mistake ?

I also tried to use this variable like

"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'))]"

then i got below error

{"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"}}]}}

Solution

  • variable section -

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

    evaluates true when skuName as P1 is selected else false. So you can just add more condition with 'OR' operators and that should work.

    Reference - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-logical#if

    [EDIT 1 ]

    Using 'OR' conditions for multiple values-

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

    Now I believe you can just put more equals(parameters('skuName'), 'VALUE')) after putting a comma based on your need.

    I have tested below one ARM Template and this works as expected-

        "$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')]"
            }
        }
    }
    

    enter image description here enter image description here

    which is expected as per the logic. And also -

    enter image description here enter image description here

    Try it out yourself at https://portal.azure.com/#create/Microsoft.Template before marking this question as helpful/not helpful.