ARM 模板的逻辑函数Logical functions for ARM templates

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

andand

and(arg1, arg2, ...)

检查所有参数值是否均为 true。Checks whether all parameter values are true.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes booleanboolean 要检查是否为 true 的第一个值。The first value to check whether is true.
arg2arg2 Yes booleanboolean 要检查是否为 true 的第二个值。The second value to check whether is true.
其他参数additional arguments No booleanboolean 用于检查是否为 true 的其他参数。Additional arguments to check whether are true.

返回值Return value

如果所有值均为 true,则返回 True;否则返回 False 。Returns True if all values are true; otherwise, False .

示例Examples

以下示例模板演示如何使用逻辑函数。The following example template shows how to use logical functions.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [ ],
    "outputs": {
        "andExampleOutput": {
            "value": "[and(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "orExampleOutput": {
            "value": "[or(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "notExampleOutput": {
            "value": "[not(bool('true'))]",
            "type": "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
andExampleOutputandExampleOutput BoolBool FalseFalse
orExampleOutputorExampleOutput BoolBool TrueTrue
notExampleOutputnotExampleOutput BoolBool FalseFalse

boolbool

bool(arg1)

将参数转换为布尔值。Converts the parameter to a boolean.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 字符串或整数string or int 要转换为布尔值的值。The value to convert to a boolean.

返回值Return value

转换后的值的布尔值。A boolean of the converted value.

备注Remarks

还可以使用 true()false() 来获取布尔值。You can also use true() and false() to get boolean values.

示例Examples

以下示例模板演示如何对字符串或整数使用 bool。The following example template shows how to use bool with a string or integer.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "trueString": {
            "value": "[bool('true')]",
            "type" : "bool"
        },
        "falseString": {
            "value": "[bool('false')]",
            "type" : "bool"
        },
        "trueInt": {
            "value": "[bool(1)]",
            "type" : "bool"
        },
        "falseInt": {
            "value": "[bool(0)]",
            "type" : "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
trueStringtrueString BoolBool TrueTrue
falseStringfalseString BoolBool FalseFalse
trueInttrueInt BoolBool TrueTrue
falseIntfalseInt BoolBool FalseFalse

falsefalse

false()

返回 false。Returns false.

参数Parameters

false 函数不接受任何参数。The false function doesn't accept any parameters.

返回值Return value

一个布尔值,该值始终为 false。A boolean that is always false.

示例Example

下面的示例返回一个 false 输出值。The following example returns a false output value.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "falseOutput": {
            "value": "[false()]",
            "type" : "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
falseOutputfalseOutput BoolBool FalseFalse

ifif

if(condition, trueValue, falseValue)

根据条件为 true 或 false 返回值。Returns a value based on whether a condition is true or false.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
conditioncondition Yes booleanboolean 要检查是为 true 还是为 false 的值。The value to check whether it's true or false.
trueValuetrueValue Yes 字符串、int、对象或数组string, int, object, or array 条件为 true 时返回的值。The value to return when the condition is true.
falseValuefalseValue Yes 字符串、int、对象或数组string, int, object, or array 条件为 false 时返回的值。The value to return when the condition is false.

返回值Return value

如果第一个参数为 True,则返回第二个参数;否则返回第三个参数 。Returns second parameter when first parameter is True ; otherwise, returns third parameter.

备注Remarks

条件为 True 时,仅评估 true 值。When the condition is True , only the true value is evaluated. 条件为 False 时,仅评估 false 值。When the condition is False , only the false value is evaluated. 使用 if 函数时,可以包含仅在特定条件下有效的表达式。With the if function, you can include expressions that are only conditionally valid. 例如,可以引用一个资源,该资源在某个条件下存在,在另一个条件下不存在。For example, you can reference a resource that exists under one condition but not under the other condition. 以下部分显示了一个条件性评估表达式的示例。An example of conditionally evaluating expressions is shown in the following section.

示例Examples

以下示例模板演示如何使用 if 函数。The following example template shows how to use the if function.

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

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

名称Name 类型Type ValueValue
yesOutputyesOutput StringString yes
noOutputnoOutput StringString no
objectOutputobjectOutput ObjectObject { "test": "value1" }{ "test": "value1" }

以下示例模板演示了如何将此函数与仅在特定条件下有效的表达式配合使用。The following example template shows how to use this function with expressions that are only conditionally valid.

{
    "$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')))]",
            "name": "[concat(parameters('vmName'),'/omsOnboarding')]",
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "location": "[parameters('location')]",
            "apiVersion": "2017-03-30",
            "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, json('null'))]"
                },
                "protectedSettings": {
                    "workspaceKey": "[if(not(empty(parameters('logAnalytics'))), listKeys(parameters('logAnalytics'), '2015-11-01-preview').primarySharedKey, json('null'))]"
                }
            }
        }
    ],
    "outputs": {
        "mgmtStatus": {
            "type": "string",
            "value": "[if(not(empty(parameters('logAnalytics'))), 'Enabled monitoring for VM!', 'Nothing to enable')]"
        }
    }
}

notnot

not(arg1)

将布尔值转换为其相反值。Converts boolean value to its opposite value.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes booleanboolean 要转换的值。The value to convert.

返回值Return value

参数为 False 时返回 True 。Returns True when parameter is False . 参数为 True 时返回 False 。Returns False when parameter is True .

示例Examples

以下示例模板演示如何使用逻辑函数。The following example template shows how to use logical functions.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [ ],
    "outputs": {
        "andExampleOutput": {
            "value": "[and(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "orExampleOutput": {
            "value": "[or(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "notExampleOutput": {
            "value": "[not(bool('true'))]",
            "type": "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
andExampleOutputandExampleOutput BoolBool FalseFalse
orExampleOutputorExampleOutput BoolBool TrueTrue
notExampleOutputnotExampleOutput BoolBool FalseFalse

以下 示例模板结合使用 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

or

or(arg1, arg2, ...)

检查任何参数值是否为 true。Checks whether any parameter value is true.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes booleanboolean 要检查是否为 true 的第一个值。The first value to check whether is true.
arg2arg2 Yes booleanboolean 要检查是否为 true 的第二个值。The second value to check whether is true.
其他参数additional arguments No booleanboolean 用于检查是否为 true 的其他参数。Additional arguments to check whether are true.

返回值Return value

如果任何值为 true,则返回 True;否则返回 False 。Returns True if any value is true; otherwise, False .

示例Examples

以下示例模板演示如何使用逻辑函数。The following example template shows how to use logical functions.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [ ],
    "outputs": {
        "andExampleOutput": {
            "value": "[and(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "orExampleOutput": {
            "value": "[or(bool('true'), bool('false'))]",
            "type": "bool"
        },
        "notExampleOutput": {
            "value": "[not(bool('true'))]",
            "type": "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
andExampleOutputandExampleOutput BoolBool FalseFalse
orExampleOutputorExampleOutput BoolBool TrueTrue
notExampleOutputnotExampleOutput BoolBool FalseFalse

true

true()

返回 true。Returns true.

参数Parameters

true 函数不接受任何参数。The true function doesn't accept any parameters.

返回值Return value

一个布尔值,该值始终为 true。A boolean that is always true.

示例Example

下面的示例返回一个 true 输出值。The following example returns a true output value.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "trueOutput": {
            "value": "[true()]",
            "type" : "bool"
        }
    }
}

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

名称Name 类型Type ValueValue
trueOutputtrueOutput BoolBool TrueTrue

后续步骤Next steps