Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using conditions in Azure ARM templates

Is there any way to use conditional statements in templates?

for example I am building template which will have vms with data disks on QA and Production, but no data disks on Dev. Another scenario would be there are some extensions only needs to be installed in prod VMs but no where else.

Any help is appreciated.

like image 304
nitinb Avatar asked Mar 09 '16 18:03

nitinb


People also ask

How do I apply a condition in ARM template?

Add the following line to the beginning of the storage account definition. "condition": "[equals(parameters('newOrExisting'),'new')]", The condition checks the value of the parameter newOrExisting . If the parameter value is new, the deployment creates the storage account.

What type of syntax is used with Azure ARM templates?

The basic syntax of the Azure Resource Manager template (ARM template) is JavaScript Object Notation (JSON). However, you can use expressions to extend the JSON values available within the template.

Can we use nested Azure ARM templates?

For linked or nested templates, you can only set the deployment mode to Incremental. However, the main template can be deployed in complete mode.


1 Answers

You can leverage the newly released comparison functions to accomplish most of this.

Here is an example of how you would use a parameter to determine if a storage account should be deployed.

Parameter:

"deployStorage": {
  "type": "string"
},

Resource:

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

Notice the new condition property in the resource along with the most recent API version for the storage provider.

Reference: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-comparison

like image 162
dbarkol Avatar answered Oct 14 '22 00:10

dbarkol