Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
资源管理器提供了多个用于在 Azure 资源管理器模板(ARM 模板)中进行比较的函数:
联合
coalesce(arg1, arg2, arg3, ...)
从参数中返回第一个非 null 值。 空字符串、空数组和空对象不为 null。
在 Bicep 中,改用 ??
运算符。 请参阅联合 ??。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 整数、字符串、数组或对象 | 要测试是否为 null 的第一个值。 |
其他参数 | 否 | 整数、字符串、数组或对象 | 要测试是否为 null 的其他值。 |
返回值
第一个非 null 参数的值,可以是字符串、整数、数组或对象。 如果所有参数都为 null,则为 null。
示例:
以下示例模板显示了不同用途的 coalesce
输出:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"objectToTest": {
"type": "object",
"defaultValue": {
"null1": null,
"null2": null,
"string": "default",
"int": 1,
"object": { "first": "default" },
"array": [ 1 ]
}
}
},
"resources": [
],
"outputs": {
"stringOutput": {
"type": "string",
"value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').string)]"
},
"intOutput": {
"type": "int",
"value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').int)]"
},
"objectOutput": {
"type": "object",
"value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').object)]"
},
"arrayOutput": {
"type": "array",
"value": "[coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2, parameters('objectToTest').array)]"
},
"emptyOutput": {
"type": "bool",
"value": "[empty(coalesce(parameters('objectToTest').null1, parameters('objectToTest').null2))]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
stringOutput | 字符串 | 默认 |
intOutput | int (整数) | 1 |
objectOutput | 物体 | {“first”: “default”} |
arrayOutput | 数组 | [1] |
emptyOutput | 布尔值 | 真 实 |
等于
equals(arg1, arg2)
检查两个值是否相同。 比较是区分大小写的。
在 Bicep 中,改用 ==
运算符。 请参阅等于 = =。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | 整数、字符串、数组或对象 | 要检查是否相等的第一个值。 |
arg2 | 是的 | 整数、字符串、数组或对象 | 要检查是否相等的第二个值。 |
返回值
如果值相等,返回 True;否则返回 False。
注解
函数 equals
通常与元素一起使用 condition
,以测试是否部署了资源:
{
"condition": "[equals(parameters('newOrExisting'),'new')]",
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2022-09-01",
"location": "[resourceGroup().location]",
"sku": {
"name": "[variables('storageAccountType')]"
},
"kind": "Storage",
"properties": {}
}
示例:
以下示例检查不同类型的值是否相等。 所有默认值返回 True:
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"firstInt": {
"type": "int",
"defaultValue": 1
},
"secondInt": {
"type": "int",
"defaultValue": 1
},
"firstString": {
"type": "string",
"defaultValue": "demo"
上述示例中的默认值输出为:
名称 | 类型 | 价值 | 注释 |
---|---|---|---|
checkInts | 布尔值 | 真 实 | |
checkStrings | 布尔值 | 假 | 结果是 false ,因为比较区分大小写。 |
checkArrays | 布尔值 | 真 实 | |
checkObjects | 布尔值 | 真 实 |
以下示例模板使用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 | 布尔值 | 真 实 |
大
greater(arg1, arg2)
检查第一个值是否大于第二个值。
在 Bicep 中,改用 >
运算符。 请参阅大于 >。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | int 或 string | 用于大于比较的第一个值。 |
arg2 | 是的 | int 或 string | 用于大于比较的第二个值。 |
返回值
如果第一个值大于第二个值,返回 True;否则返回 False。
示例:
以下示例检查一个值是否大于另一个值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"firstInt": {
"type": "int",
"defaultValue": 1
},
"secondInt": {
"type": "int",
"defaultValue": 2
},
"firstString": {
"type": "string",
"defaultValue": "A"
},
"secondString": {
"type": "string",
"defaultValue": "a"
}
},
"resources": [
],
"outputs": {
"checkInts": {
"type": "bool",
"value": "[greater(parameters('firstInt'), parameters('secondInt') )]"
},
"checkStrings": {
"type": "bool",
"value": "[greater(parameters('firstString'), parameters('secondString'))]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
checkInts | 布尔值 | 假 |
checkStrings | 布尔值 | 真 实 |
greaterOrEquals
greaterOrEquals(arg1, arg2)
检查第一个值是否大于或等于第二个值。
在 Bicep 中,改用 >=
运算符。 请参阅大于或等于 >=。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | int 或 string | 用于大于或等于比较的第一个值。 |
arg2 | 是的 | int 或 string | 用于大于或等于比较的第二个值。 |
返回值
如果第一个值大于或等于第二个值,返回 True;否则返回 False。
示例:
以下示例检查一个值是否大于或等于另一个值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"firstInt": {
"type": "int",
"defaultValue": 1
},
"secondInt": {
"type": "int",
"defaultValue": 2
},
"firstString": {
"type": "string",
"defaultValue": "A"
},
"secondString": {
"type": "string",
"defaultValue": "a"
}
},
"resources": [
],
"outputs": {
"checkInts": {
"type": "bool",
"value": "[greaterOrEquals(parameters('firstInt'), parameters('secondInt') )]"
},
"checkStrings": {
"type": "bool",
"value": "[greaterOrEquals(parameters('firstString'), parameters('secondString'))]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
checkInts | 布尔值 | 假 |
checkStrings | 布尔值 | 真 实 |
少
less(arg1, arg2)
检查第一个值是否小于第二个值。
在 Bicep 中,改用 <
运算符。 请参阅小于 <。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | int 或 string | 用于小于比较的第一个值。 |
arg2 | 是的 | int 或 string | 用于小于比较的第二个值。 |
返回值
如果第一个值小于第二个值,返回 True;否则返回 False。
示例:
以下示例检查一个值是否小于另一个值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"firstInt": {
"type": "int",
"defaultValue": 1
},
"secondInt": {
"type": "int",
"defaultValue": 2
},
"firstString": {
"type": "string",
"defaultValue": "A"
},
"secondString": {
"type": "string",
"defaultValue": "a"
}
},
"resources": [
],
"outputs": {
"checkInts": {
"type": "bool",
"value": "[less(parameters('firstInt'), parameters('secondInt') )]"
},
"checkStrings": {
"type": "bool",
"value": "[less(parameters('firstString'), parameters('secondString'))]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
checkInts | 布尔值 | 真 实 |
checkStrings | 布尔值 | 假 |
lessOrEquals
lessOrEquals(arg1, arg2)
检查第一个值是否小于或等于第二个值。
在 Bicep 中,改用 <=
运算符。 请参阅小于或等于 <=。
参数
参数 | 必选 | 类型 | DESCRIPTION |
---|---|---|---|
arg1 | 是的 | int 或 string | 用于小于或等于比较的第一个值。 |
arg2 | 是的 | int 或 string | 用于小于或等于比较的第二个值。 |
返回值
如果第一个值小于或等于第二个值,返回 True;否则返回 False。
示例:
以下示例检查一个值是否小于或等于另一个值:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"firstInt": {
"type": "int",
"defaultValue": 1
},
"secondInt": {
"type": "int",
"defaultValue": 2
},
"firstString": {
"type": "string",
"defaultValue": "A"
},
"secondString": {
"type": "string",
"defaultValue": "a"
}
},
"resources": [
],
"outputs": {
"checkInts": {
"type": "bool",
"value": "[lessOrEquals(parameters('firstInt'), parameters('secondInt') )]"
},
"checkStrings": {
"type": "bool",
"value": "[lessOrEquals(parameters('firstString'), parameters('secondString'))]"
}
}
}
上述示例中的默认值输出为:
名称 | 类型 | 价值 |
---|---|---|
checkInts | 布尔值 | 真 实 |
checkStrings | 布尔值 | 假 |
后续步骤
有关 ARM 模板中各部分的说明,请参阅了解 ARM 模板的结构和语法。