0

从资源组定义的标签命名tag1(这样做是通过门户网站),我在Visual Studio中的下列部署模板:获取从资源组标签在Azure的资源管理器模板

{ 
     "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。

回答

-1

您的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

希望这有助于!

+0

我通过门户创建了标签,并在运行部署之前验证标签是否存在。但是,如果标签不存在同样的错误,那么你是对的。有趣的发现:出于某种原因,当我部署时,标签**被清除。你如何设置你的标签?通过门户或PowerShell或REST? – edteke

+0

我通过门户设置 – juvchan

+0

我也是,有趣的发现:我看到你也从门户运行部署模板,我从Visual Studio运行它。它从门户网站运行,不适用于Visual Studio。看起来Visual Studio在部署导致问题时会清除资源组标记。这是一个不同的问题 – edteke

相关问题