资源管理器提供了多个用于在 Azure 资源管理器模板(ARM 模板)中进行比较的函数:
和
and(arg1, arg2, ...)
检查所有参数值是否为 true。
Bicep 不支持 and
函数。 请改用 && 运算符。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 布尔 | 要检查它是否为 true 的第一个值。 |
arg2 | 是的 | 布尔 | 要检查其是否为 true 的第二个值。 |
其他参数 | 否 | 布尔 | 用于检查它们是否为真的更多参数。 |
返回值
如果所有值均为 true,则返回 True;否则返回 False。
例子
以下示例演示如何使用函数 logical
:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"andExampleOutput": {
"type": "bool",
"value": "[and(bool('true'), bool('false'))]"
},
"orExampleOutput": {
"type": "bool",
"value": "[or(bool('true'), bool('false'))]"
},
"notExampleOutput": {
"type": "bool",
"value": "[not(bool('true'))]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
andExampleOutput | 布尔值 | 假 |
orExampleOutput | 布尔值 | 真 实 |
notExampleOutput | 布尔值 | 假 |
布尔
bool(arg1)
将参数转换为布尔值。
在 Bicep 文件中,请使用 bool 逻辑函数。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 字符串或整数 | 要转换为布尔值的值。 |
返回值
转换后的值的布尔值。
注解
还可以使用 true() 和 false() 来获取布尔值。
例子
以下示例演示如何与字符串或整数一 bool
起使用:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"trueString": {
"type": "bool",
"value": "[bool('true')]"
},
"falseString": {
"type": "bool",
"value": "[bool('false')]"
},
"trueInt": {
"type": "bool",
"value": "[bool(1)]"
},
"falseInt": {
"type": "bool",
"value": "[bool(0)]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
trueString | 布尔值 | 真 实 |
falseString | 布尔值 | 假 |
trueInt | 布尔值 | 真 实 |
falseInt | 布尔值 | 假 |
假
false()
返回 false。
false
函数在 Bicep 中不可用。 请改用 false
关键字。
参数
该 false
函数不接受任何参数。
返回值
一个布尔值,始终为 false。
示例:
以下示例返回 false 输出值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"falseOutput": {
"type": "bool",
"value": "[false()]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
falseOutput | 布尔值 | 假 |
如果
if(condition, trueValue, falseValue)
根据条件为 true 或 false 返回值。
Bicep 不支持 if
函数。 请改用 ?: 运算符。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
条件 | 是的 | 布尔 | 要检查其是否为 true 或 false 的值。 |
trueValue | 是的 | 字符串、int、对象或数组 | 条件为 true 时返回的值。 |
falseValue | 是的 | 字符串、int、对象或数组 | 条件为 false 时返回的值。 |
返回值
如果第一个参数为 True,则返回第二个参数;否则返回第三个参数。
注解
条件为 True 时,仅评估 true 值。 条件为 False 时,仅评估 false 值。 使用 if
函数时,可以包含仅在特定条件下有效的表达式。 例如,可以引用一个资源,该资源在某个条件下存在,在另一个条件下不存在。 以下部分显示了一个条件性评估表达式的示例。
请注意,上述规则仅适用于在模板部署过程开始时评估条件的情况。 例如,不能在条件中使用 reference()
。
例子
以下示例演示如何使用函数 if
:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
],
"outputs": {
"yesOutput": {
"type": "string",
"value": "[if(equals('a', 'a'), 'yes', 'no')]"
},
"noOutput": {
"type": "string",
"value": "[if(equals('a', 'b'), 'yes', 'no')]"
},
"objectOutput": {
"type": "object",
"value": "[if(equals('a', 'a'), json('{\"test\": \"value1\"}'), json('null'))]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
yesOutput | 字符串 | 是的 |
noOutput | 字符串 | 否 |
objectOutput | 物体 | { “test”: “value1” } |
以下示例模板演示了如何将此函数与仅在特定条件下有效的表达式配合使用。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": {
"type": "string"
},
"location": {
"type": "string"
},
"logAnalytics": {
"type": "string",
"defaultValue": ""
}
},
"resources": [
{
"condition": "[not(empty(parameters('logAnalytics')))]",
"type": "Microsoft.Compute/virtualMachines/extensions",
"apiVersion": "2022-11-01",
"name": "[format('{0}/omsOnboarding', parameters('vmName'))]",
"location": "[parameters('location')]",
"properties": {
"publisher": "Microsoft.EnterpriseCloud.Monitoring",
"type": "MicrosoftMonitoringAgent",
"typeHandlerVersion": "1.0",
"autoUpgradeMinorVersion": true,
"settings": {
"workspaceId": "[if(not(empty(parameters('logAnalytics'))), reference(parameters('logAnalytics'), '2015-11-01-preview').customerId, null())]"
},
"protectedSettings": {
"workspaceKey": "[if(not(empty(parameters('logAnalytics'))), listKeys(parameters('logAnalytics'), '2015-11-01-preview').primarySharedKey, null())]"
}
}
}
],
"outputs": {
"mgmtStatus": {
"type": "string",
"value": "[if(not(empty(parameters('logAnalytics'))), 'Enabled monitoring for VM!', 'Nothing to enable')]"
}
}
}
不
not(arg1)
将布尔值转换为其相反值。
Bicep 不支持 not
函数。 请改用 ! 运算符。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 布尔 | 要转换的值。 |
返回值
参数为 False 时返回 True。 参数为 True 时返回 False。
例子
以下示例演示如何使用函数 logical
:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"andExampleOutput": {
"type": "bool",
"value": "[and(bool('true'), bool('false'))]"
},
"orExampleOutput": {
"type": "bool",
"value": "[or(bool('true'), bool('false'))]"
},
"notExampleOutput": {
"type": "bool",
"value": "[not(bool('true'))]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
andExampleOutput | 布尔值 | 假 |
orExampleOutput | 布尔值 | 真 实 |
notExampleOutput | 布尔值 | 假 |
以下示例使用not
等于:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
],
"outputs": {
"checkNotEquals": {
"type": "bool",
"value": "[not(equals(1, 2))]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
checkNotEquals | 布尔值 | 真 实 |
或
or(arg1, arg2, ...)
检查任何参数值是否为 true。
Bicep 不支持 or
函数。 请改用 || 运算符。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 布尔 | 要检查它是否为 true 的第一个值。 |
arg2 | 是的 | 布尔 | 要检查其是否为 true 的第二个值。 |
其他参数 | 否 | 布尔 | 用于检查它们是否为真的更多参数。 |
返回值
如果任何值为 true,则返回 True;否则返回 False。
例子
以下示例演示如何使用函数 logical
:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"andExampleOutput": {
"type": "bool",
"value": "[and(bool('true'), bool('false'))]"
},
"orExampleOutput": {
"type": "bool",
"value": "[or(bool('true'), bool('false'))]"
},
"notExampleOutput": {
"type": "bool",
"value": "[not(bool('true'))]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
andExampleOutput | 布尔值 | 假 |
orExampleOutput | 布尔值 | 真 实 |
notExampleOutput | 布尔值 | 假 |
是
true()
返回 true。
true
函数在 Bicep 中不可用。 请改用 true
关键字。
参数
该 true
函数不接受任何参数。
返回值
一个布尔值,始终为 true。
示例:
以下示例返回真正的输出值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"trueOutput": {
"type": "bool",
"value": "[true()]"
}
}
}
前述示例的输出为:
名称 | 类型 | 价值 |
---|---|---|
trueOutput | 布尔值 | 真 实 |
后续步骤
- 有关 ARM 模板中各部分的说明,请参阅了解 ARM 模板的结构和语法。