Monday, November 7, 2022

How to manage resource tags using azure cli and frame conditions on that

 update tag in vm: https://learn.microsoft.com/en-us/azure/virtual-machines/tag-cli


tag any resource azure: https://learn.microsoft.com/en-us/cli/azure/tag?view=azure-cli-latest




To list all tags in a VM:


az vm show --resource-group myresourcegroup --name Installshield --query tags




update a tag in a VM:


az vm update --resource-group myresourcegroup --name Installshield --set tags.purpose="Build Process of DM and Mart" tags.usage=0




Above is specific to VM resource, below commands is for any type of resource, but using VMs for explanation.




to list all tags:


az tag list --resource-id /subscriptions/mysubscriptionid/resourcegroups/myresourcegroup/providers/Microsoft.Compute/virtualMachines/Installshield




update a tag:




az tag update --resource-id /subscriptions/mysubscriptionid/resourcegroups/myresourcegroup/providers/Microsoft.Compute/virtualMachines/Installshield --operation replace --tags usage=1




Get the value of a tag, using jq command for linux.




az tag list --resource-id /subscriptions/mysubscriptionid/resourcegroups/myresourcegroup/providers/Microsoft.Compute/virtualMachines/Installshield | jq -r '.properties.tags.usage'


you can store it to a variable also:


insstatus="$(az tag list --resource-id /subscriptions/mysubscriptionid/resourcegroups/myresourcegroup/providers/Microsoft.Compute/virtualMachines/Installshield | jq -r '.properties.tags.usage')"




or can frame conditions also based on the value:




if [ -z ${insstatus+x} ]; 


then 


  echo "insstatus is unset"; 


else 


  echo "insstatus is set to '$insstatus'"; 


  if [ $insstatus -ge 1 ]; then


    echo "greater than or equal to 0"


  else


    echo "zero or negative"


  fi


fi




((insstatus=insstatus+1))






if [ -z ${insstatus+x} ]; 


then 


  echo "insstatus is unset"; 


else 


  echo "insstatus is set to '$insstatus'"; 


  if [ $insstatus -ge 1 ]; then


    echo "greater than or equal to 0"


  else


    echo "zero or negative"


  fi


fi




Reference links:


https://linuxize.com/post/bash-increment-decrement-variable/


https://unix.stackexchange.com/questions/212183/how-do-i-check-if-a-variable-exists-in-an-if-statement


https://askubuntu.com/questions/1042659/how-to-check-if-a-value-is-greater-than-or-equal-to-another




json links:


https://unix.stackexchange.com/questions/493834/how-to-extract-a-value-from-json-response-using-shell-script


https://medium.com/swlh/different-ways-to-handle-json-in-a-linux-shell-81183bc2c9bc


No comments:

Post a Comment