Working With Azure CLI 2.0

This is a sequel to my previous post of Introduction to Azure CLI 2.0 and its Installation, where I talked about why we need Azure CLI 2.0 and what’s exactly changed. Azure CLI 2.0 is a Command Line Utility and now we are going to look into the command.
Azure
Azure CLI 2.0 uses the keyword az whereas Azure XPlat CLI uses Azure so both the utilities can run on your system simultaneously. If you have been using Azure XPlat CLI, the transaction will not be difficult and you just have to use the above mantra az noun verb.

For eg if you want to list the VMs in an account then say

az vm list

Here VM is the Service or the noun and the list is the operation or the verb in the above methodology.

Getting started with the account

Login

When we run this command, then it will generate an authentication key for me and we need to go to the URL alias ms/devicelogin and paste the code generated by the Command Line. Login to my Azure account to tell my command line to use the account.

Once you’re into the account, it’s going to give you the details about all the subscriptions associated with the account. Now you need to set one account.

az account set

Azure

I have used the subscription ID to set as my default subscription but you can use the name of the subscription as well.

Now to verify, we can use az account show command given below.

Azure

Working with Resource Groups

Let us create a new Resource Group.

Azure

To check all the existing groups

az group list

Now, the command given above gave me a lot of data and it was pretty hard to take out the information from it. Hence we will talk about how to format the output and query it.

Output

To format the output, we have 4 options. I am going to use az storage account list command but I am going to add –out or –o to tell my command line to format 

  • TSV (TAB Spaced Values)
    It is going to format the output in tab space values. It’s suitable when you have less content and you’re querying it on some particular property.

    az storage account list –out tsv

    Azure

  • Table
    As the name suggests, this is going to format the output in a tabular form and its suitable for the big data with the multiple properties.

    Azure

  • JSON
    Now all of us work with JSON day in and day out so it is my default output format as I can directly take out the output in a JSON and pass it to some other tool/Application as per the requirement.

    Azure

  • Jsonc
    Jsonc is nothing but the same JSON but in the color coded format, which is more readable from the user prospective as well as useful as it is in the same JSON format that is usable across.

    Azure

You can set your default output format by using the command given below.

az configure –output jsonc

Azure

As I needed some colors in my life and screen, I opted for the jsonc and pressed 2 as my choice.

Query

As we are getting a lot of data, we need a mechanism to query. For Querying, Azure CLI 2.0 uses a utility call JMESPath It's a query language for JSON, which is popular amongst the open source and AWS developer. You can directly query from the command prompt or pass the output to the jmespath terminal and use the JMESPath query structure. Let us check out the query in the command line

We need to use Query to query the data and as we are getting the data in the form of an array, so we need to say –query “[].PropertyName

Let us run this for the storage account list and take out just the names of the account

az storage account list --query “[].name”

Azure

As we can see, we are only getting the name of the storage accounts, as expected. In this example, I am using the svccat as 4 out of 5 storage accounts have it in its name and piping, the output to further query down to the name of the storage account as without it we are going to get all the details of the 4 storage account queried in the first query.

Azure

Summary

We saw that writing a query is so easy in this Azure CLI 2.0 and with the az noun verb nomenclature becomes quite easy and even if you don’t know the command, you can type –h for all the available commands associated with that group. Also, we looked into the output formats and query the outputs in the command line itself.

Up Next
    Ebook Download
    View all
    Learn
    View all