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

使用 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 订阅 ID 的环境变量。 若要获取 Azure 订阅 ID,请使用:

    az account show --name 'your subscription name' --query id -o tsv
    

    若要设置值,请使用适用于你的环境的选项。

    setx AZURE_SUBSCRIPTION_ID your-subscription-id
    

    注意

    如果只需要访问当前正在运行的控制台中的环境变量,则可以使用 set(而不是 setx)设置环境变量。

    添加环境变量后,可能需要重启任何正在运行的、需要读取环境变量的程序(包括控制台窗口)。 例如,如果使用 Visual Studio 作为编辑器,请在运行示例之前重启 Visual Studio。

应用标记

Azure Python 提供了 ResourceManagementClient.tags.begin_create_or_update_at_scope 方法来应用标记。 它替换资源、资源组或订阅中的所有标记。 调用该命令时,请传入要标记的实体的资源 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"
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 标记从 Normal 更改为 Green 。

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 方法并传递实体的资源 ID。

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

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。 传递要删除的标记的资源 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"
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)

后续步骤