Search code examples
azureazure-rm-template

Azure ARM - If Condition


Within our Azure environment we have a naming standard for the subscription name but this hasn't always been followed which means we have the following:

1) <companyName>-<environment>-<businessUnit>
2) <companyName>-<role>

As examples:

1) MYCOMPANY-PRD-IT
2) MYCOMPANY-SANDBOX

Within my ARM template I pull out the environment info from the subscription (PRD in example 1) but this fails when I try to deploy to something like example 2, SANDBOX. The following is an example of the variables used in the template and it is the 'subXXX' variables I am having issues with as searches for the first and last '-' in the subscription name to find the environment:

"variables": {
        "rgName": "[resourceGroup().name]",
        "rgNameStart": "[substring(variables('rgName'), 0, lastIndexOf(variables('rgName'), '-'))]",
        "subName": "[subscription().displayName]",
        "subNameLengthFirst": "[add(indexOf(variables('subName'), '-'), 1)]",
        "subNameLengthLast": "[lastIndexOf(variables('subName'), '-')]",
        "subEnvironmentLength": "[sub(variables('subNameLengthLast'), variables('subNameLengthFirst'))]",
        "subEnvironment": "[substring(variables('subName'), variables('subNameLengthFirst'), variables('subEnvironmentLength'))]",
        "storageAccountName": "[concat('sa', replace(replace(variables('rgNameStart'), 'RG-', ''), '-', ''))]",
        "storageAccountNameGenerated": "[toLower(concat(variables('storageAccountName'), parameters('storageAccountRole'), parameters('storageAccountInt')))]"
    },

What I need to do is something like "if subscription name = MYCOMPANY-SANDBOX then ..... else ....."

I have checked out the Microsoft info on conditions in ARM templates but not the easiest to follow so was hoping someone here had a real life example that helps.

Thanks.


Solution

  • You can use RM functions in you ARM. Check this .

    Below function will return value based on your condition.

    if(condition, trueValue, falseValue)