【问题标题】:Concatenate objects in ARM Template连接 ARM 模板中的对象
【发布时间】:2018-07-18 01:42:50
【问题描述】:

我正在尝试根据这篇文章在 ARM 模板中设置一些标签:https://docs.microsoft.com/en-us/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 吗?
  • @LachieWhite 添加了带有注释的代码示例,解释了我正在尝试做什么。

标签: azure azure-resource-manager arm-template


【解决方案1】:

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

【讨论】:

  • 有没有办法在没有变量声明的情况下做到这一点?
  • "tags": "[union(parameters('tagValues'),json('{\"myExtraTag\":\"myTagValue \"}'))]", 我在答案中也给出了这个选项。这不使用变量。如果您不想使用parameters,那么您可以添加另一个json 表达式并在2 个json 对象之间应用union
  • 有没有办法动态地做到这一点?使用union,您必须对所有要应用联合的对象进行硬编码。如果您想对长度未知的对象数组执行此操作怎么办?
  • @hansmbakker 如果您发布一个带有示例模板的新问题,这将很容易回答。
【解决方案2】:

好问题格雷格!

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

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


希望这会有所帮助!

【讨论】:

  • 这不是我想要的——希望能够为不同的资源连接不同的值。我已经修改了问题以使这一点更清楚,你是对的,我从哪里得到了这个例子 - 这是我问题中的链接!
  • 啊,看看你现在的意思,将尝试一些事情,看看我是否可以让它工作,如果可以,我会编辑答案:)
  • 祝你好运 - 我尝试了一些方法,但不知道如何连接对象,或者即使这是一种明智的方法。试图使用这种方法来简化可维护性,但可能不值得。
  • 我已经编辑了一种不带标签对象的方法,这降低了可维护性,但可以工作,只是对每个资源进行更多的手动复制和粘贴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多