在ARM模板中连接对象

14

我正在尝试按照此文章中的方式在ARM模板中设置一些标记:https://learn.microsoft.com/zh-cn/azure/azure-resource-manager/resource-manager-templates-resources#apply-an-object-to-the-tag-element

我想在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": {}
        }
  ]
}

嘿,格雷格,我相信这是可能的。你能分享一些你在问题中使用的JSON吗? - Lachie White
1
@LachieWhite添加了带有注释的代码示例,解释了我试图做什么。 - Greg the Incredulous
2个回答

11

使用 union 函数可以实现。您可以在这里找到更多相关文档。

以下解决方案可能适用于您。我提供了两种方法。一种是将内联字符串转换为对象,使用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": {}
        }
  ]
}

有没有一种方法可以在不声明变量的情况下完成这个操作? - Don Scott
1
"tags": "[union(parameters('tagValues'),json('{"myExtraTag":"myTagValue "}'))]", 我在答案中也提供了这个选项。如果你不想使用 parameters,那么可以添加另一个 json 表达式,并在两个 json 对象之间应用 union - Venkata Dorisala
有没有一种动态地实现这个的方法? 使用 union,您必须硬编码要应用联合的所有对象。如果您想对长度未知的对象数组执行此操作怎么办? - hansmbakker
@hansmbakker 如果您在新问题中发布一个带有示例模板的问题,那么回答将会很容易。 - Venkata Dorisala
@Venky 发布在 https://dev59.com/F7vpa4cB1Zd3GeqPCPoZ:如何在 ARM 模板中将数组转换为一个对象的属性 - hansmbakker

1

很好的问题,Greg!

您可以通过以下方式实现您想要的效果:

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

在此输入图片描述
希望这能有所帮助!


这不是我想要的 - 我想能够连接不同资源的不同值。我已经修改了问题以使其更清晰,你说得对,我在问题中提供了示例链接! - Greg the Incredulous
啊,现在我明白你的意思了,我会尝试几个方法,看看能否让它工作,如果可以的话,我会编辑答案 :) - Lachie White
祝你好运 - 我尝试过一些方法,但无法弄清如何连接对象,甚至不确定这是否是明智的方法。我试图使用这种方法来简化可维护性,但可能并不值得。 - Greg the Incredulous
我已经编辑了一种无需标签对象的方法,这降低了可维护性,但可以工作,只需要更多手动复制和粘贴每个资源。 - Lachie White

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接