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"

다음 단계