Azure Classroom Series – 16/Apr/2020

How to query Azure CLI for specific results

  • Examples:

    1. Give me the names of vms in the resource group
    2. Give me the public ips of the vm
  • Pre-req’s:

    • JSON:
  • What we will be learning:

Scenarios:

  1. 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 Preview Preview
    • 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}'
    
  2. 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}"
    
  3. Using basic functions

    • write an azure cli to find the count of resource groups in central us
    az group list --query "length([?location == 'centralus'])"
    
  4. 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
  1. Write an azure cli query to get the public ip address of the vm
    • Note: combine two cli queries.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin