ARM 模板的比较函数Comparison functions for ARM templates

资源管理器提供了多个用于在 Azure 资源管理器 (ARM) 模板中进行比较的函数。Resource Manager provides several functions for making comparisons in your Azure Resource Manager (ARM) templates.

coalescecoalesce

coalesce(arg1, arg2, arg3, ...)

从参数中返回第一个非 null 值。Returns first non-null value from the parameters. 空字符串、空数组和空对象不为 null。Empty strings, empty arrays, and empty objects are not null.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int、string、array 或 objectint, string, array, or object 要测试是否为 null 的第一个值。The first value to test for null.
其他参数additional args No int、string、array 或 objectint, string, array, or object 要测试是否为 null 的其他值。Additional values to test for null.

返回值Return value

第一个非 null 参数的值,可以是字符串、整数、数组或对象。The value of the first non-null parameters, which can be a string, int, array, or object. 如果所有参数都为 null,则为 null。Null if all parameters are null.

示例Example

以下示例模板显示 coalesce 不同用法的输出。The following example template shows the output from different uses of 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))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
stringOutputstringOutput StringString 默认值default
intOutputintOutput intInt 11
objectOutputobjectOutput ObjectObject {"first": "default"}{"first": "default"}
arrayOutputarrayOutput ArrayArray [1][1]
emptyOutputemptyOutput BoolBool TrueTrue

equalsequals

equals(arg1, arg2)

检查两个值是否相等。Checks whether two values equal each other.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int、string、array 或 objectint, string, array, or object 要检查是否相等的第一个值。The first value to check for equality.
arg2arg2 Yes int、string、array 或 objectint, string, array, or object 要检查是否相等的第二个值。The second value to check for equality.

返回值Return value

如果值相等,返回 True;否则返回 FalseReturns True if the values are equal; otherwise, False.

备注Remarks

equals 函数通常与 condition 元素一起使用来测试资源是否已部署。The equals function is often used with the condition element to test whether a resource is deployed.

{
    "condition": "[equals(parameters('newOrExisting'),'new')]",
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageAccountName')]",
    "apiVersion": "2017-06-01",
    "location": "[resourceGroup().location]",
    "sku": {
        "name": "[variables('storageAccountType')]"
    },
    "kind": "Storage",
    "properties": {}
}

示例Example

下列示例模板检查不同类型的值是否相等。The following example template checks different types of values for equality. 所有默认值都返回 True。All the default values return 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": "a"
        },
        "secondString": {
            "type": "string",
            "defaultValue": "a"
        },
        "firstArray": {
            "type": "array",
            "defaultValue": ["a", "b"]
        },
        "secondArray": {
            "type": "array",
            "defaultValue": ["a", "b"]
        },
        "firstObject": {
            "type": "object",
            "defaultValue": {"a": "b"}
        },
        "secondObject": {
            "type": "object",
            "defaultValue": {"a": "b"}
        }
    },
    "resources": [
    ],
    "outputs": {
        "checkInts": {
            "type": "bool",
            "value": "[equals(parameters('firstInt'), parameters('secondInt') )]"
        },
        "checkStrings": {
            "type": "bool",
            "value": "[equals(parameters('firstString'), parameters('secondString'))]"
        },
        "checkArrays": {
            "type": "bool",
            "value": "[equals(parameters('firstArray'), parameters('secondArray'))]"
        },
        "checkObjects": {
            "type": "bool",
            "value": "[equals(parameters('firstObject'), parameters('secondObject'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
checkIntscheckInts BoolBool TrueTrue
checkStringscheckStrings BoolBool TrueTrue
checkArrayscheckArrays BoolBool TrueTrue
checkObjectscheckObjects BoolBool TrueTrue

以下示例模板结合使用 notequalsThe following example template uses not with equals.

{
    "$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))]"
        }
    }
}

前述示例的输出为:The output from the preceding example is:

名称Name 类型Type ValueValue
checkNotEqualscheckNotEquals BoolBool TrueTrue

greatergreater

greater(arg1, arg2)

检查第一个值是否大于第二个值。Checks whether the first value is greater than the second value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int 或 stringint or string 用于大于比较的第一个值。The first value for the greater comparison.
arg2arg2 Yes int 或 stringint or string 用于大于比较的第二个值。The second value for the greater comparison.

返回值Return value

如果第一个值大于第二个值,返回 True;否则返回 FalseReturns True if the first value is greater than the second value; otherwise, False.

示例Example

下列示例模板检查一个值是否大于另一个值。The following example template checks whether the one value is greater than the other.

{
    "$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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
checkIntscheckInts BoolBool FalseFalse
checkStringscheckStrings BoolBool TrueTrue

greaterOrEqualsgreaterOrEquals

greaterOrEquals(arg1, arg2)

检查第一个值是否大于或等于第二个值。Checks whether the first value is greater than or equal to the second value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int 或 stringint or string 用于大于或等于比较的第一个值。The first value for the greater or equal comparison.
arg2arg2 Yes int 或 stringint or string 用于大于或等于比较的第二个值。The second value for the greater or equal comparison.

返回值Return value

如果第一个值大于或等于第二个值,返回 True;否则返回 FalseReturns True if the first value is greater than or equal to the second value; otherwise, False.

示例Example

下列示例模板检查一个值是否大于等于另一个值。The following example template checks whether the one value is greater than or equal to the other.

{
    "$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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
checkIntscheckInts BoolBool FalseFalse
checkStringscheckStrings BoolBool TrueTrue

lessless

less(arg1, arg2)

检查第一个值是否小于第二个值。Checks whether the first value is less than the second value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int 或 stringint or string 用于小于比较的第一个值。The first value for the less comparison.
arg2arg2 Yes int 或 stringint or string 用于小于比较的第二个值。The second value for the less comparison.

返回值Return value

如果第一个值小于第二个值,返回 True;否则返回 FalseReturns True if the first value is less than the second value; otherwise, False.

示例Example

下列示例模板检查一个值是否小于另一个值。The following example template checks whether the one value is less than the other.

{
    "$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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
checkIntscheckInts BoolBool TrueTrue
checkStringscheckStrings BoolBool FalseFalse

lessOrEqualslessOrEquals

lessOrEquals(arg1, arg2)

检查第一个值是否小于或等于第二个值。Checks whether the first value is less than or equal to the second value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes int 或 stringint or string 用于小于或等于比较的第一个值。The first value for the less or equals comparison.
arg2arg2 Yes int 或 stringint or string 用于小于或等于比较的第二个值。The second value for the less or equals comparison.

返回值Return value

如果第一个值小于或等于第二个值,返回 True;否则返回 FalseReturns True if the first value is less than or equal to the second value; otherwise, False.

示例Example

下列示例模板检查一个值是否小于等于另一个值。The following example template checks whether the one value is less than or equal to the other.

{
    "$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'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
checkIntscheckInts BoolBool TrueTrue
checkStringscheckStrings BoolBool FalseFalse

后续步骤Next steps