使用 Python 套用標籤
本文說明如何使用 Python 來標記資源、資源群組和訂用帳戶。 如需標籤建議及限制事項,請參閱使用標籤整理 Azure 資源和管理階層。
必要條件
已安裝 Python 3.8 或更新版本。 若要安裝最新版,請參閱 Python.org
下列適用於 Python 的 Azure 程式庫套件已安裝在虛擬環境中。 若要安裝任何套件,請使用
pip install {package-name}
- azure-identity
- azure-mgmt-resource
如果虛擬環境中已安裝這些舊版套件,您可能需要使用
pip install --upgrade {package-name}
更新這些套件本文中的範例會使用 CLI 型驗證 (
AzureCliCredential
)。 視環境而定,您可能需要先執行az login
才能進行驗證。使用 Azure 訂用帳戶識別碼的環境變數。 若要取得 Azure 訂用帳戶識別碼,請使用:
az account show --name 'your subscription name' --query id -o tsv
若要設定值,請使用適合您環境的選項。
套用標記
Azure Python 提供 ResourceManagementClient.tags.begin_create_or_update_at_scope 方法來套用標籤。 其會取代資源、資源群組或訂用帳戶上的所有標籤。 呼叫命令後,會傳入您要標記的實體資源識別碼。
下列範例會將一組標記套用至儲存體帳戶:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
storage_account_name = "demostore"
tags = {
"Dept": "Finance",
"Status": "Normal"
}
tag_resource = TagsResource(
properties={'tags': tags}
)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_client.tags.begin_create_or_update_at_scope(resource.id, tag_resource)
print(f"Tags {tag_resource.properties.tags} were added to resource with ID: {resource.id}")
如果您再次執行命令,但這次使用不同的標籤,請注意之前的標籤會消失。
tags = {
"Team": "Compliance",
"Environment": "Production"
}
若要將標籤新增至已有標籤的資源,請使用 ResourceManagementClient.tags.begin_update_at_scope。 在 TagsPatchResource 物件上,將 operation
參數設定為 Merge
。
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
storage_account_name = "demostore"
tags = {
"Dept": "Finance",
"Status": "Normal"
}
tag_patch_resource = TagsPatchResource(
operation="Merge",
properties={'tags': tags}
)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_client.tags.begin_update_at_scope(resource.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were added to existing tags on resource with ID: {resource.id}")
請注意,現有的標籤會隨著新增的兩個新標籤成長。
每個標記名稱只能有一個值。 如果提供標籤新的值,即使您使用合併作業,新的標籤仍會取代舊值。 下列範例會將 Status
標記從「一般」變更為「綠色」。
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
storage_account_name = "demostore"
tags = {
"Status": "Green"
}
tag_patch_resource = TagsPatchResource(
operation="Merge",
properties={'tags': tags}
)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_client.tags.begin_update_at_scope(resource.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were added to existing tags on resource with ID: {resource.id}")
將 operation
參數設為 Replace
後,新的標籤集會取代現有的標籤。
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
storage_account_name = "demostore"
tags = {
"Project": "ECommerce",
"CostCenter": "00123",
"Team": "Web"
}
tag_patch_resource = TagsPatchResource(
operation="Replace",
properties={'tags': tags}
)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_client.tags.begin_update_at_scope(resource.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} replaced tags on resource with ID: {resource.id}")
只有新的標記會保留在資源上。
相同的命令也會使用資源群組或訂用帳戶。 將標籤傳入您要標記的資源群組或訂用帳戶的識別碼。 若要將新的標記集新增至資源群組,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
tags = {
"Dept": "Finance",
"Status": "Normal"
}
tag_resource = TagsResource(
properties={'tags': tags}
)
resource_group = resource_client.resource_groups.get(resource_group_name)
resource_client.tags.begin_create_or_update_at_scope(resource_group.id, tag_resource)
print(f"Tags {tag_resource.properties.tags} were added to resource group: {resource_group.id}")
若要更新資源群組的標記,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
tags = {
"CostCenter": "00123",
"Environment": "Production"
}
tag_patch_resource = TagsPatchResource(
operation="Merge",
properties={'tags': tags}
)
resource_group = resource_client.resource_groups.get(resource_group_name)
resource_client.tags.begin_update_at_scope(resource_group.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were added to existing tags on resource group: {resource_group.id}")
若要更新訂用帳戶的標記,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
tags = {
"Team": "Web Apps"
}
tag_patch_resource = TagsPatchResource(
operation="Merge",
properties={'tags': tags}
)
resource_client.tags.begin_update_at_scope(f"/subscriptions/{subscription_id}", tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were added to subscription: {subscription_id}")
在資源群組中,您可能會有多個資源具有相同的名稱。 在此情況下,您可以使用下列命令來設定每個資源:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
tags = {
"Dept": "IT",
"Environment": "Test"
}
tag_patch_resource = TagsPatchResource(
operation="Merge",
properties={'tags': tags}
)
resources = resource_client.resources.list_by_resource_group(resource_group_name, filter="name eq 'sqlDatabase1'")
for resource in resources:
resource_client.tags.begin_update_at_scope(resource.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were added to resource: {resource.id}")
列出標籤
若要取得資源、資源群組或訂用帳戶的標籤,請使用 ResourceManagementClient.tags.get_at_scope 方法,並傳入實體的資源識別碼。
若要查看資源的標記,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group_name = "demoGroup"
storage_account_name = "demostorage"
resource_client = ResourceManagementClient(credential, subscription_id)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_tags = resource_client.tags.get_at_scope(resource.id)
print (resource_tags.properties.tags)
若要查看資源群組的標記,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group = resource_client.resource_groups.get("demoGroup")
resource_group_tags = resource_client.tags.get_at_scope(resource_group.id)
print (resource_group_tags.properties.tags)
若要查看訂用帳戶的標記,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
subscription_tags = resource_client.tags.get_at_scope(f"/subscriptions/{subscription_id}")
print (subscription_tags.properties.tags)
依標記列出
若要取得具有特定標記名稱和值的資源,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resources = resource_client.resources.list(filter="tagName eq 'CostCenter' and tagValue eq '00123'")
for resource in resources:
print(resource.name)
若要取得具有特定標記名稱與任何標記值的資源,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resources = resource_client.resources.list(filter="tagName eq 'Dept'")
for resource in resources:
print(resource.name)
若要取得具有特定標記名稱和值的資源群組,請使用:
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_groups = resource_client.resource_groups.list(filter="tagName eq 'CostCenter' and tagValue eq '00123'")
for resource_group in resource_groups:
print(resource_group.name)
移除標記
若要移除特定標籤,請將 operation
設定為 Delete
。 傳入您要刪除標籤的資源識別碼。
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import TagsPatchResource
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
resource_group_name = "demoGroup"
storage_account_name = "demostore"
tags = {
"Dept": "IT",
"Environment": "Test"
}
tag_patch_resource = TagsPatchResource(
operation="Delete",
properties={'tags': tags}
)
resource = resource_client.resources.get_by_id(
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
"2022-09-01"
)
resource_client.tags.begin_update_at_scope(resource.id, tag_patch_resource)
print(f"Tags {tag_patch_resource.properties.tags} were removed from resource: {resource.id}")
即會移除指定的標記。
若要移除所有標籤,請使用 ResourceManagementClient.tags.begin_delete_at_scope 方法。
import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
subscription = resource_client.subscriptions.get(subscription_id)
resource_client.tags.begin_delete_at_scope(subscription.id)
下一步
- 並非所有資源類型都支援標記。 若要判斷您是否可以將標籤套用至資源類型,請參閱 Azure 資源的標籤支援。
- 如需如何實作標記策略的建議,請參閱資源命名和標記決策指南。
- 如需標籤建議及限制事項,請參閱使用標籤整理 Azure 資源和管理階層。