Conditional parts in ARM Templates

When creating reusable ARM templates you have a number of options on how to manage conditional parts in your templates. The smallest conditions can be done by parameters, medium differences can be done by  t-shirt sizes and large differences by linked templates. In this blog post I’ll show how to use implement conditions by linked templates.

Making conditions with linked templates
From one template in resource manager you can link to an other template. This enables you to decompose a large template into smaller more maintainable templates. The linking is done by the template type Microsoft.Resources/deployments. This template contains a property templateLink with the uri to the actual template.

"resources": [
  {
      "apiVersion": "2015-01-01",
      "name": "linkedTemplate",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "incremental",
        "templateLink": {
          "uri": "https://www.contoso.com/AzureTemplates/newStorageAccount.json",
          "contentVersion": "1.0.0.0"
        },
        "parameters": {
          "StorageAccountName":{"value": "[parameters('StorageAccountName')]"}
        }
      }
  }
]

There are now more ways to add conditions to ARM templates:
New ways to support conditions in ARM templates

By making the uri changeable you can chose on which template you want to load. The uri can be a variable that you compose base on a given parameter. A simple example can be:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters":{
       "condition":{
          "type":"string",
          "allowedValues":{
              "option1",
              "option2"
          }
       },
       ...
    },
    "variables":{
        "conditionaluri":"[concat('https://www.contoso.com/AzureTemplates/newStorageAccount',parameters('condition'),'.json')]",
...
    }
    "resources": [
      {
          "apiVersion": "2015-01-01",
          "name": "linkedTemplate",
          "type": "Microsoft.Resources/deployments",
          "properties": {
            "mode": "incremental",
            "templateLink": {
              "uri": "[variables('conditionaluri')]",
              "contentVersion": "1.0.0.0"
            },
            "parameters": {
              "StorageAccountName":{"value": "[parameters('StorageAccountName')]"}
            }
          }
      }
    ]
}

In this template the parameter condition will choose between the uri’s:

Both templates can contain different implementations of the StorageAccount template. This technique can be used on all conditions you want to implement into your templates. It can be applied on multiple levels in your linked template tree.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: