【问题标题】:Get tags from a Resource Group in an Azure Resource Manager Template从 Azure 资源管理器模板中的资源组获取标签
【发布时间】:2017-05-05 22:12:03
【问题描述】:

从一个定义了名为 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')]"
      }
    }

我得到这个错误:

...'语言表达式属性'tags'不存在,可用属性是'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。

【问题讨论】:

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


    【解决方案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]"
        }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我通过门户创建了标签,并在运行部署之前验证了标签是否存在。但是如果标签不存在,你是对的,同样的错误也会发生。有趣的发现:由于某种原因,当我部署时标签被清除。你如何设置你的标签?通过门户、PowerShell 或 REST?
    • 我通过门户设置
    • 我也是,有趣的发现:我看到您也从门户运行了部署模板,而我是从 Visual Studio 运行的。它适用于门户,不适用于 Visual Studio。似乎 Visual Studio 在部署导致问题时清除了资源组标记。这是一个不同的问题
    • 是的,我从 Azure 门户进行了模板部署。如果我的回答对您有帮助,请将其标记为答案并有帮助。希望您的问题现在得到解决。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多