你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
资源管理器提供了多个用于在 Azure 资源管理器模板(ARM 模板)中进行比较的函数:
联合
coalesce(arg1, arg2, arg3, ...)
从参数中返回第一个非 null 值。 空字符串、空数组和空对象不为 NULL。
在 Bicep 中,改用 ?? 运算符。 请参阅联合 ??。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| arg1 | 是 | int、string、array 或 object | 要测试是否为 null 的第一个值。 | 
| 其他参数 | 否 | int、string、array 或 object | 要测试是否为 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 | String | 默认 | 
| intOutput | int | 1 | 
| objectOutput | Object | {“first”: “default”} | 
| arrayOutput | Array | [1] | 
| emptyOutput | Bool | True | 
equals
equals(arg1, arg2)
检查两个值是否相同。 比较是区分大小写的。
在 Bicep 中,改用 == 运算符。 请参阅等于 = =。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| arg1 | 是 | int、string、array 或 object | 要检查是否相等的第一个值。 | 
| arg2 | 是 | int、string、array 或 object | 要检查是否相等的第二个值。 | 
返回值
如果值相等,返回 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 | Bool | True | |
| checkStrings | Bool | False | 结果是 false,因为比较区分大小写。 | 
| checkArrays | Bool | True | |
| checkObjects | Bool | True | 
以下示例模板使用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 | Bool | True | 
greater
greater(arg1, arg2)
检查第一个值是否大于第二个值。
在 Bicep 中,改用 > 运算符。 请参阅大于 >。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 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 | Bool | False | 
| checkStrings | Bool | True | 
greaterOrEquals
greaterOrEquals(arg1, arg2)
检查第一个值是否大于或等于第二个值。
在 Bicep 中,改用 >= 运算符。 请参阅大于或等于 >=。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 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 | Bool | False | 
| checkStrings | Bool | True | 
less
less(arg1, arg2)
检查第一个值是否小于第二个值。
在 Bicep 中,改用 < 运算符。 请参阅小于 <。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 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 | Bool | True | 
| checkStrings | Bool | False | 
lessOrEquals
lessOrEquals(arg1, arg2)
检查第一个值是否小于或等于第二个值。
在 Bicep 中,改用 <= 运算符。 请参阅小于或等于 <=。
parameters
| 参数 | 必选 | 类型 | 说明 | 
|---|---|---|---|
| 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 | Bool | True | 
| checkStrings | Bool | False | 
后续步骤
有关 ARM 模板中各部分的说明,请参阅了解 ARM 模板的结构和语法。