In my recent client I had a feature to get the list of private IP addresses of Azure virtual machines based on theirs tags.
For apply this I used the Azure cli tools and execute this command
az vm list-ip-addresses --ids $(az resource list --query "[?type=='Microsoft.Compute/virtualMachines' && tags.ENV == 'DEV'].id" --output tsv) --query "[].virtualMachine.network.privateIpAddresses[0]" | jq -r @csv
Explanation:
The sub part of this command:
az resource list --query "[?type=='Microsoft.Compute/virtualMachines' && tags.ENV == 'DEV'].id" --output tsv
return all ID of resources of types VirtualMachines that have the tag ENV = DEV.
We can add more tag filter for e.g.
tags.ENV == 'DEV' && tags.TYPE == 'SQL'
This result is in input of the command
az vm list-ip-addresses --ids <result>
the result is list of IP addresses properties
I also filtered this result for get only private address IP by adding --query
option by filtered the 1st private address IP
--query "[].virtualMachine.network.privateIpAddresses[0]"
That it’s the essential of the command.
For by feature I added jq for more formated result as string list with comma separator as jq -r @csv
And the final result is:
That’ all.
Now we get dynamically the IP list based on tags we can apply this for multiple use cases for example :
- to use Nsg rules source or destination range IP based on tags VM.
- export this on CSV file for monitoring
- …
Comments