你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure CLI 应用标记

本文介绍如何使用 Azure CLI 标记资源、资源组和订阅。 有关标记建议和限制,请参阅 使用标记来组织 Azure 资源和管理层次结构

添加标签

Azure CLI 提供两个命令来应用标记: az tag createaz tag update。 需要具有 Azure CLI 2.10.0 版本或更高版本。 您可以使用 az version 检查您的版本。 若要更新或安装它,请参阅 安装 Azure CLI

替换 az tag create 资源、资源组或订阅上的所有标记。 调用命令时,传递要标记的实体的资源 ID。

以下示例将一组标记应用于存储帐户:

resource=$(az resource show -g demoGroup -n demostorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv)
az tag create --resource-id $resource --tags Dept=Finance Status=Normal

命令完成后,请注意资源有两个标记。

"properties": {
  "tags": {
    "Dept": "Finance",
    "Status": "Normal"
  }
},

如果再次运行该命令,但这次使用不同的标记,请注意早期标记会消失。

az tag create --resource-id $resource --tags Team=Compliance Environment=Production
"properties": {
  "tags": {
    "Environment": "Production",
    "Team": "Compliance"
  }
},

若要将标记添加到已具有标记的资源,请使用 az tag update。 将 --operation 参数设置为 Merge

az tag update --resource-id $resource --operation Merge --tags Dept=Finance Status=Normal

请注意,现有标记随着添加两个新标记而增长。

"properties": {
  "tags": {
    "Dept": "Finance",
    "Environment": "Production",
    "Status": "Normal",
    "Team": "Compliance"
  }
},

每个标记名称只能有一个值。 如果为标记提供新值,则新标记将替换旧值,即使使用合并作也是如此。 以下示例将 Status 标记从 Normal 更改为 Green

az tag update --resource-id $resource --operation Merge --tags Status=Green
"properties": {
  "tags": {
    "Dept": "Finance",
    "Environment": "Production",
    "Status": "Green",
    "Team": "Compliance"
  }
},

将参数--operation设置为Replace时,新标记集将替换现有标记。

az tag update --resource-id $resource --operation Replace --tags Project=ECommerce CostCenter=00123 Team=Web

只有新标记保留在资源上。

"properties": {
  "tags": {
    "CostCenter": "00123",
    "Project": "ECommerce",
    "Team": "Web"
  }
},

相同的命令也适用于资源组或订阅。 将它们传入要标记的资源组或订阅的标识符。

若要向资源组添加新的标记集,请使用:

group=$(az group show -n demoGroup --query id --output tsv)
az tag create --resource-id $group --tags Dept=Finance Status=Normal

若要更新资源组的标记,请使用:

az tag update --resource-id $group --operation Merge --tags CostCenter=00123 Environment=Production

若要向订阅添加新的标记集,请使用:

sub=$(az account show --subscription "Demo Subscription" --query id --output tsv)
az tag create --resource-id /subscriptions/$sub --tags CostCenter=00123 Environment=Dev

若要更新订阅的标记,请使用:

az tag update --resource-id /subscriptions/$sub --operation Merge --tags Team="Web Apps"

列出标记

若要获取资源、资源组或订阅的标记,请使用 az tag list 命令并传递实体的资源 ID。

若要查看资源的标记,请使用:

resource=$(az resource show -g demoGroup -n demostorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv)
az tag list --resource-id $resource

若要查看资源组的标记,请使用:

group=$(az group show -n demoGroup --query id --output tsv)
az tag list --resource-id $group

若要查看订阅的标记,请使用:

sub=$(az account show --subscription "Demo Subscription" --query id --output tsv)
az tag list --resource-id /subscriptions/$sub

按标记列出

若要获取具有特定标记名称和值的资源,请使用:

az resource list --tag CostCenter=00123 --query [].name

若要获取具有特定标签名称且具有任意标签值的资源,请使用:

az resource list --tag Team --query [].name

若要获取具有特定标记名称和值的资源组,请使用:

az group list --tag Dept=Finance

删除标记

若要删除特定标记,请使用 az tag update 并设置为 --operationDelete. 请传递要删除的标签的资源 ID。

az tag update --resource-id $resource --operation Delete --tags Project=ECommerce Team=Web

已删除指定的标记。

"properties": {
  "tags": {
    "CostCenter": "00123"
  }
},

若要删除所有标记,请使用 az tag delete 命令。

az tag delete --resource-id $resource

处理空格

如果标记名称或值包含空格,请将它们括在引号中。

az tag update --resource-id $group --operation Merge --tags "Cost Center"=Finance-1222 Location="West US"

后续步骤