Getting started 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 command
az noun verb
Azure Cli 2.0 uses the keyword az whereas azure Xplat cli uses azure so both of the utilities can run on your system simultaneously. If you have been using the azure Xplat ClI then the transaction will not be difficult 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 List is the operation or the verb in the above methodology.
Getting Started with the account
When we run this command then it will generate an authentication key for me and we need to go to the url aka.ms/devicelogin and paste the code generated by the Command Line and then login into my azure account to tell my command line to use that account.
Once you’re into the account it’s going to give you the details about all the subscriptions associated with that account. Now you need to set one account.
az account set
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 the az account show command
Working with Resource Groups
let us create a new Resource Group
az group create –name DemoRG –location westus
To check all the existing groups
az group list
Now this above command gave me a lot of data and it was pretty hard to take out the Information from it. So we will talk about how to format the output and query it.
So to format the output we have 4 options. I am going to use the az storage account list command but I am going to add –out or –o to tell my command line to format the
- TSV (TAB Spaced Values)
It is going to format the output in a 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
As the Name suggest that its going to format the output in a tabular form and its suitable for big data with multiple properties.
Now all of us works with JSON day in and dayout 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 requirement.
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.
Setting up default output format
You can set your default output format by using the following command
az configure –output jsonc
As I needed some colors in my life and screen, I opted for the jsonc and pressed 2 as my choice.
As we are getting a lot of data so we need a mechanism to query. For Querying, Azure CLI 2.0 uses a utility call JMESPath, its an query language for JSON 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 command line
We need to use –Query to query the data and as we are getting the data in the form of 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”
As we can see we are only getting the name of the storage accounts as expected. Now if you want to search for the specific records so you can also use contains. In this example I am using the svccat as 4 out of 5 storage account has 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.
We saw that writing query is so easy in this azure cli 2.0 and with the az noun verb nomenclature its becomes quite easy and even if you don’t know the command you can type –h for all the available command associated with that group. Also, we looked into the Output formats and the query the outputs in the command line itself.