繁体   English   中英

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

[英]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')]"
},

evaluates true when skuName as P1 is selected else false. 因此,您可以使用“或”运算符添加更多条件,这应该可以工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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