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

标签 azure azure-resource-manager azure-rm-template azure-resource-group

从定义了名为 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/

相关文章:

Azure计费API : Resource RateCard & Resource Usage

每个环境的 Azure 数据工厂动态内容默认值

azure - 从ARM模板获取API管理的公共(public)IP地址

azure - MSB1003 : Specify a project or solution file. 当前工作目录不包含项目或解决方案文件

.net - 在 Azure 云中部署不安全代码

azure - ARM模板: Get an App Client Id by either App Name or App ID URI

Azure 资源管理器 microsoft.classicstorage/storageAccounts 没有注册资源提供程序

azure - 我可以有条件地使用ARM模板中的复制功能吗

azure - AWS 中的服务到服务身份验证 - 最佳实践是什么?

azure - 获取 PnPUnifiedGroup : Code: Authorization_RequestDenied