ARM模板连接对象

时间:2018-07-18 01:42:50

标签: azure arm-template

我正在尝试根据本文https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-templates-resources#apply-an-object-to-the-tag-element

在ARM模板中设置一些标签。

我希望能够在TagValues参数中设置几个通用标签,然后为其他特定资源附加其他标签。这有可能吗?我已经尝试过使用[concat()],但是它对处理对象不满意,并且验证失败。

这是我要做的事的一个例子:

{
  "$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": {}
        }
  ]
}

2 个答案:

答案 0 :(得分:2)

可以使用union函数。您可以找到关于它的更多文档here

以下解决方案可能对您有用。我给出了两种方法。一个内联字符串,通过json函数转换为对象。另一种方法是在变量中创建一个对象,并使用union连接两个对象。

    {
  "$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": {}
        }
  ]
}

答案 1 :(得分:1)

好问题,格雷格!

您可以通过以下方法实现目标:

{
  "$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
希望这会有所帮助!