使用 Azure CLI 套用標籤

本文說明如何使用 Azure CLI 來標記資源、資源群組和訂用帳戶。 如需標記的建議及限制事項,請參閱使用標籤組織 Azure 資源和管理階層

套用標記

Azure CLI 提供兩個命令來套用標籤:az tag createaz tag update。 您必須有 Azure CLI 2.10.0 版或更新版本。 您可以使用 az version 來檢查您的版本。 若要更新或安裝,請參閱安裝 Azure CLI

az tag create 會取代資源、資源群組或訂用帳戶上的所有標記。 呼叫命令後,會傳入您要標記的實體資源識別碼。

下列範例會將一組標記套用至儲存體帳戶:

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 標記從「一般」變更為「綠色」

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 命令,並傳遞實體的資源識別碼。

若要查看資源的標記,請使用:

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 並將 --operation 設定為 Delete。 傳入您要刪除標籤的資源識別碼。

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"

下一步