gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:21:08 26 4
gpt4 key购买 nike

我一直在研究 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 作为 P1 时,评估结果为 true,否则评估结果为 false。因此,您可以使用“OR”运算符添加更多条件,这样就可以了。

引用 - https://learn.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')]"
}
}
}

enter image description here enter image description here

这符合逻辑。还有-

enter image description here enter image description here

亲自尝试一下 https://portal.azure.com/#create/Microsoft.Template在将此问题标记为有帮助/无帮助之前。

关于azure - 如何在ARM模板中设置变量的值? Or 在 ARM 模板中使用 Or 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70235286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com