Logical functions for Azure Resource Manager templates

Resource Manager provides several functions for making comparisons in your templates.

and

and(arg1, arg2, ...)

Checks whether all parameter values are true.

Parameters

Parameter Required Type Description
arg1 Yes boolean The first value to check whether is true.
arg2 Yes boolean The second value to check whether is true.
additional arguments No boolean Additional arguments to check whether are true.

Return value

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/2015-01-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 Value
andExampleOutput Bool False
orExampleOutput Bool True
notExampleOutput Bool False

bool

bool(arg1)

Converts the parameter to a boolean.

Parameters

Parameter Required Type Description
arg1 Yes string or int The value to convert to a boolean.

Return value

A boolean of the converted value.

Examples

The following example template shows how to use bool with a string or integer.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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 Value
trueString Bool True
falseString Bool False
trueInt Bool True
falseInt Bool False

if

if(condition, trueValue, falseValue)

Returns a value based on whether a condition is true or false.

Parameters

Parameter Required Type Description
condition Yes boolean The value to check whether it's true or false.
trueValue Yes string, int, object, or array The value to return when the condition is true.
falseValue Yes string, int, object, or array The value to return when the condition is false.

Return value

Returns second parameter when first parameter is True; otherwise, returns third parameter.

Remarks

When the condition is True, only the true value is evaluated. When the condition is False, only the false value is evaluated. 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

The following example template shows how to use the if function.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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 Value
yesOutput String yes
noOutput String no
objectOutput Object { "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/2015-01-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')]"
        }
    }
}

not

not(arg1)

Converts boolean value to its opposite value.

Parameters

Parameter Required Type Description
arg1 Yes boolean The value to convert.

Return value

Returns True when parameter is 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/2015-01-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 Value
andExampleOutput Bool False
orExampleOutput Bool True
notExampleOutput Bool False

The following example template uses not with equals.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-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 Value
checkNotEquals Bool True

or

or(arg1, arg2, ...)

Checks whether any parameter value is true.

Parameters

Parameter Required Type Description
arg1 Yes boolean The first value to check whether is true.
arg2 Yes boolean The second value to check whether is true.
additional arguments No boolean Additional arguments to check whether are true.

Return value

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/2015-01-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 Value
andExampleOutput Bool False
orExampleOutput Bool True
notExampleOutput Bool False

Next steps