Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM Template concatenate objects

I am trying to set up some tags within an ARM template in accordance with this article: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-templates-resources#apply-an-object-to-the-tag-element

I wanted to be able to set up a couple of generic tags in the TagValues parameter, but then append others for specific resources. Is this possible, and if so how? I've tried using [concat()] but it's not happy dealing with objects, and fails validation.

Here's an example of what I'm trying to do:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "tagValues": {
      "type": "object",
      "defaultValue": {
        "Dept": "Finance",
        "Environment": "Production"
      }
    }
  },
  "resources": [
    {
      "apiVersion": "2016-01-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[concat('storage', uniqueString(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "tags": "[parameters('tagValues')]",     // want to concatenate another tag here, so that the following is returned: "Dept": "Finance", "Environment": "Production", "myExtraTag": "myTagValue"
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
{
          "apiVersion": "2016-01-01",
          "type": "Microsoft.Storage/storageAccounts",
          "name": "mySecondResource",
          "location": "[resourceGroup().location]",
          "tags": "[parameters('tagValues')]",     // want to concatenate a DIFFERENT tag here, so that the following is returned: "Dept": "Finance", "Environment": "Production", "myExtraDifferentTag": "myDifferentTagValue"
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "Storage",
          "properties": {}
        }
  ]
}
like image 918
Greg the Incredulous Avatar asked Jul 18 '18 01:07

Greg the Incredulous


People also ask

How do you use depends on in ARM template?

dependsOn. Within your Azure Resource Manager template (ARM template), the dependsOn element enables you to define one resource as a dependent on one or more resources. Its value is a JavaScript Object Notation (JSON) array of strings, each of which is a resource name or ID.

Can ARM template update existing resource?

ARM template willnot recreate/overwrite the existing resource, if the resource is specified in the template. It will update the resource if the property values for a resource are changed.

What is the syntax of list key function in ARM?

The syntax for this function varies by name of the list operations. Each implementation returns values for the resource type that supports a list operation. The operation name must start with list and may have a suffix. Some common usages are list , listKeys , listKeyValue , and listSecrets .


2 Answers

It's possible with union function. You can find more documentation about it here

Below solution might work for you. I have given 2 approaches. One with inline string converted to object with json function. Other approach is to create an object in variables and using union to concatenate both objects.

    {
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "tagValues": {
      "type": "object",
      "defaultValue": {
        "Dept": "Finance",
        "Environment": "Production"
      }
    }
  },
  "variables" : {
     "customTag" : {"myExtraDifferentTag": "myDifferentTagValue", "myAnotherExtraDifferentTag": "myAnotherDifferentTagValue"}
  },
  "resources": [
    {
      "apiVersion": "2016-01-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[concat('storage', uniqueString(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "tags": "[union(parameters('tagValues'),json('{\"myExtraTag\":\"myTagValue \"}'))]",  //Concatenates `tagValues` object to inline object    
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
{
          "apiVersion": "2016-01-01",
          "type": "Microsoft.Storage/storageAccounts",
          "name": "mySecondResource",
          "location": "[resourceGroup().location]",
          "tags": "[union(parameters('tagValues'),variables('customTag'))]",     // Concatenates `tagValues` object to `customTag` object
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "Storage",
          "properties": {}
        }
  ]
}
like image 197
Venky Avatar answered Oct 09 '22 16:10

Venky


Good question Greg!

You can achieve what you are after with the below:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
      "testvar": "customtagfromvar"
  },
  "parameters": {
  },
  "resources": [
    {
      "apiVersion": "2016-01-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[concat('storage', uniqueString(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "tags": {
          "department": "Finance",
          "customTag": "[concat(variables('testvar'), '-concatedtext')]"
      },
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
  ]
}

enter image description here
Hope this helps!

like image 44
Lachie White Avatar answered Oct 09 '22 18:10

Lachie White