简体   繁体   中英

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

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://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-logical#if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM