Blog

Conditional parts in ARM Templates

07 Dec, 2016
Xebia Background Header Wave

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

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": "https://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.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts