0

我正在寻找在 Azure 中使用 python SDK 更新资源标签的帮助。我能够更新标签,resource groups但我无法找到如何更新标签resources

下面是我用来更新resource groups标签的代码

from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
import os
credential = AzureCliCredential()

subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] = "Key"

#resource_group = os.getenv("RESOURCE_GROUP_NAME", "name")

resource_client = ResourceManagementClient(credential, subscription_id)

group_list = resource_client.resources.list()


for group in list(group_list):
    a = group.location
    b = group.name
    if not group.tags:
        resource_client.resources.create_or_update(
                b,
                {
                    "location" : a,
                    "tags" : tag_dict
                }
        )
4

1 回答 1

0

根据您的要求,我们编写了以下代码来为特定资源组下的各个资源创建标签我们已经在本地环境中测试了代码并且运行良好。

    from  logging  import  NullHandler
    from  azure.core  import  pipeline
    from  azure.identity  import  AzureCliCredential,  _credentials
    from  azure.mgmt.resource  import  ResourceManagementClient,  resources
    import  os
    
    credential  =  AzureCliCredential()
    subscription_id  =  "<subscription-id>"
    resource_group  =  "<resource-group>"
    resource_client  =  ResourceManagementClient(credential,  subscription_id)
    resource_list=  resource_client.resources.list_by_resource_group(resource_group)
    tag_dict  =  {"Month":  "sept-24"}
    for  resource  in  resource_list:
      body  =  {
        "operation"  :  "create",
         "properties"  :  {
            "tags"  :  tag_dict
          }
     }  
      resource_client.tags.create_or_update_at_scope(resource.id,body)
    print("Resource tags were updated successfully please check the output in the portal")

这是供参考的示例输出:

在此处输入图像描述

于 2021-09-24T12:04:54.650 回答