14

I am trying to set up some tags within an ARM template in accordance with this article: https://learn.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": {}
        }
  ]
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Greg the Incredulous
  • 1,676
  • 4
  • 29
  • 42

2 Answers2

11

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": {}
        }
  ]
}
Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90
  • Is there a way to do this without a variable declaration? – Don Scott Nov 17 '19 at 02:14
  • 1
    `"tags": "[union(parameters('tagValues'),json('{\"myExtraTag\":\"myTagValue \"}'))]",` I gave this option as well in the answer. This doesn't use variable. If you don't want to use `parameters` then you can add another `json` expression and apply `union` between 2 `json` objects. – Venkata Dorisala Nov 17 '19 at 10:31
  • is there a way to do this dynamically? With `union` you have to hardcode all objects you want to apply union on. What if you want to do that to an array of objects of unknown length? – hansmbakker Jul 03 '20 at 10:40
  • @hansmbakker it would be easy to answer if you post a new question with a sample template on it. – Venkata Dorisala Jul 03 '20 at 11:06
  • @Venky posted at https://stackoverflow.com/questions/62714060/how-to-convert-an-array-into-properties-of-one-object-in-an-arm-template – hansmbakker Jul 03 '20 at 11:46
1

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!

Lachie White
  • 1,246
  • 2
  • 14
  • 21
  • That's not quite what I'm looking for - want to be able to concatenate different values for different resources. I've amended question to make this clearer, and you're right about where I got the example - it's the link in my question! – Greg the Incredulous Jul 18 '18 at 02:01
  • ahh see what you mean now, will have a try at a couple things and see if i can get it working, will edit answer if i can :) – Lachie White Jul 18 '18 at 02:17
  • Good luck - I've had a bit of a go but couldn't figure out how to concatenate objects, or even if this was a sensible approach. Was trying to use this approach to ease maintainability, but may not be worth it. – Greg the Incredulous Jul 18 '18 at 02:19
  • I have edited a way to do it without the tag object, which reduces the ease of maintainability but will work, just more manual copy and paste for each resource – Lachie White Jul 18 '18 at 02:23