Reputation: 2023
I am looking for help in updating the resource tags using python SDK in azure. I am able to update tags for resource groups
but I am not able to find how I can update tags for resources
Below is the code that I have used for updating resource groups
tags
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
}
)
Upvotes: 0
Views: 1078
Reputation: 5546
Based on your requirement , we have written the below code to create the tags to the individual resources under a particular resource group We have tested the code in our local environment & it is working fine.
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")
Here is the Sample output for reference:
Upvotes: 0