How to query Azure CLI for specific results
-
Examples:
- Give me the names of vms in the resource group
- Give me the public ips of the vm
-
Pre-req’s:
- JSON:
-
What we will be learning:
Scenarios:
-
Basic Queries:
- Ensure you have atleast one vm in your azure account
- Write a query to get the list of vms in azure
az vm list
- Navigate to the documentation page and look out for global parameters
- To write a JMES query use the global parameter –query
- Best-Practice for beginers:
- Store the output of the command in the JSON File
- Get the Names of the first vm
az vm list --query '[0].name'
- Get the Names of all the vms
az vm list --query '[*].name'
- write a command to get all the resource groups
az group list
- write a query to get the last resource group name
az group list --query '[-1].name'
- write a query to get all the resource names
az group list --query '[*].name'
- Write a query to display resource group and its location
az group list --query '[*].[name, location]' az group list --output table --query '[*].{Name:name, Region: location}' az group list --query '[*].{Name:name, Region: location}' az group list --output table --query '[::2].{Name:name, Region: location}' az group list --output table --query '[::-1].{Name:name, Region: location}'
-
Filtering the Data
- Write a query which will show the resource groups from centralus only
az group list --query '[?location == 'centralus']'
- Now format outpup
az group list --output table --query "[?location == 'centralus'].{Name:name}"
-
Using basic functions
- write an azure cli to find the count of resource groups in central us
az group list --query "length([?location == 'centralus'])"
-
Write an azure cli to get the vm name, location and username of all the vms with size standard_B1s
az vm list --query "[?hardwareProfile.vmSize == 'Standard_B1s'].{VMName: name, Region: location, Username: osProfile.adminUsername}" --output table
- Write an azure cli query to get the public ip address of the vm
- Note: combine two cli queries.