fbpx

Today I’m doing an Azure Migration and consolidation for a client and they wanted to tag everything within the existing Resource Group where is it deployed before we started moving things around to other Resource Groups and Subscriptions. Two important notes:  first they didn’t mind if the RG has current TAGs, if it does then they can be added to the resources within that RG and they didn’t want to lose any of the existing TAGs on the resources.

So, first I tagged all of the Resource Groups with this tag:

KEY: OrginialRGName / Value: [NAME OF RESOURCE GROUP]

New Tag

Next, I used the following script to load all the resource groups into an array, read the currently assigned tag values on the resource group and the resources.  Finally, it will apply all the tags onto the resources which will now include the OriginalRGName tag and value.  This will be done to all resources in the subscription.


groups=$(az group list --query [].name --output tsv)
for rg in $groups
do
  jsontag=$(az group show -n $rg --query tags) || true
  t=$(echo $jsontag | tr -d '"{},' | sed 's/: /=/g') || true
  r=$(az resource list -g $rg --query [].id --output tsv) || true
  for resid in $r
  do
    jsonrtag=$(az resource show --id $resid --query tags) || true
    rt=$(echo $jsonrtag | tr -d '"{},' | sed 's/: /=/g') || true
    az resource tag --tags $t$rt --id $resid || true
  done
done

Notice how the Tags on the resource remained and my new tag, OriginalRGName is now put in place!
Apply Tags to all of your Azure Resources 1

You might get some errors on resources that don’t show in the portal or support tags, like alerts for Azure montior, but these can be ignored. Give it a try on a test subscription and have fun TAGing!

@deltadan

Microsoft MVP

Dan Patrick is the Chief Infrastructure Architect for Solliance and a 15 year veteran at Microsoft. He has an extensive background in IT Infrastructure and Operations. Dan has both architected and lead teams building and supporting some of the largest service providers in North America with as many 15,000 Windows Servers and 120 million endpoints. Dan has worked with Azure IaaS solutions extensively since 2012. He has a passion for Virtualization with deep experience leveraging Hyper-V, Vmware, and Citrix. He is also a Clustering specialist focusing on large host clusters and SQL Always On Availability Groups. Recently Dan, authored the Networking, Azure Active Directory and Containers portion of the 70-533 Exam Reference for Microsoft Press. You can follow him on Twitter @deltadan