gpt4 book ai didi

azure - 在 ARM 模板中连接对象

转载 作者:行者123 更新时间:2023-12-03 03:16:34 27 4
gpt4 key购买 nike

我正在尝试根据本文在 ARM 模板中设置一些标签:https://learn.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": {}
}
]
}

最佳答案

可以使用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": {}
}
]
}

关于azure - 在 ARM 模板中连接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51392382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com