gpt4 book ai didi

azure - 从 Azure 资源管理器模板中的资源组获取标签

转载 作者:行者123 更新时间:2023-12-01 04:48:49 25 4
gpt4 key购买 nike

从定义了名为 tag1 的标签的资源组(通过门户完成)中,我在 Visual Studio 中拥有以下部署模板:

{
"name": "appsettings",
"type": "config",
"apiVersion": "2015-08-01",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('websiteName'))]"
],
"tags": {
"displayName": "website AppSettings"
},
"properties": {
"Setting1": "[resourceGroup().tags.tag1]",
"Setting2": "[parameters('param2')]"
}
}

我收到此错误:

... 'The language expression property 'tags' doesn't exist, available properties are 'id, name, location, properties'...

但是使用 https://resources.azure.com我可以看到资源组确实具有 tag 属性:

{
"id": "/subscriptions/{someguid}/resourceGroups/resourceGroup1",
"name": "resourceGroup1",
"location": "brazilsouth",
"tags": {
"tag1": "test"
},
"properties": {
"provisioningState": "Succeeded"
}
}

有什么方法可以获取部署模板中的资源组标签吗?

更新

正如 @juvchan 指出的,标签必须存在,否则会发生此错误。我创建了标签,但是从 VisualStudio 部署模板时,标签被删除,从门户部署时,标签被保留。这导致了不同的问题。

原因是 Visual Studio 项目有一个包含以下行的 PowerShell 脚本:

#Create or update the resource group using the specified template file and template parameters file
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -Force -ErrorAction Stop

但是 New-AzureRmResourceGroup cmdlet 不会保留现有标记。意识到。解决方案:修改脚本,如果资源组已存在,则不运行 cmdlet。

最佳答案

用于获取下面资源组标签的 ARM 模板语法是正确的。

"[resourceGroup().tags.tag1]"

您需要确保您的资源组在部署 ARM 模板之前创建了上述标签,以便获取特定标签的值。

"tags": { "tag1": "test" },

当我尝试部署 ARM 模板以获取尚未创建的资源组标签时,我能够重现您的确切错误。

当我在部署之前为资源组创建标签时,我还能够按照 ARM 模板部署中的预期获取资源组标签值。

{
"id": "/subscriptions/{id}/resourceGroups/ResourceGroupwithTag",
"name": "ResourceGroupwithTag",
"location": "southeastasia",
"tags": {
"displayName": "test"
},
"properties": {
"provisioningState": "Succeeded"
}
}

我的 ARM 模板的输出部分,用于显示资源组标签。

"outputs": {
"rgTag": {
"type": "String",
"value": "[resourceGroup().tags.displayName]"
}
}

enter image description here

希望这有帮助!

关于azure - 从 Azure 资源管理器模板中的资源组获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814461/

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