ARM 模板连接对象 [英] ARM Template concatenate objects
问题描述
我正在尝试根据本文在 ARM 模板中设置一些标签:https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-templates-resources#apply-an-object-to-the-tag-element
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
我希望能够在 TagValues 参数中设置几个通用标签,然后为特定资源附加其他标签.这可能吗,如果可以,怎么办?我试过使用 [concat()] 但它不喜欢处理对象,并且验证失败.
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": {}
}
]
}
推荐答案
union
功能可以实现.您可以找到有关它的更多文档 这里
It's possible with union
function. You can find more documentation about it here
以下解决方案可能适合您.我给出了两种方法.一种使用 json
函数将内联字符串转换为对象.另一种方法是在变量中创建一个对象并使用 union
连接两个对象.
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": {}
}
]
}
这篇关于ARM 模板连接对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!