Taking the leap - Unleashing the power of Azure CLI

  • Thread starter Thread starter nadavbh
  • Start date Start date
N

nadavbh

In my previous article,

I explained the benefits of Azure CLI and some methods to tailor it to your needs.

In this article, I want to explore how to use Azure CLI efficiently to retrieve the data you want, in a quick and reliable manner.

In addition, I will share some useful advice to improve your experience with the command-line interface.



Using JMESPath with Azure CLI


Azure CLI can fit almost any scenario that you currently have using Azure.

With over 9,000 reference commands, there's always a way to achieve your goal.

Prompt use of JMESPath can enhance your way of getting the precise data that you need.



For this example, we'll use Azure Kubernetes Service (AKS).



Let's say that you're tasked with getting the number of nodes available on your cluster with their VM size, for FinOps proposes.

You've reached to a conclusion that the command should look something like this:













az aks show --resource-group <resource-group-name> --name <aks-cluster-name>













The output of this command is a long JSON file that does not meet the criteria.



You've decided to make your output more readable by using the "table" option :













az aks show --resource-group <resource-group-name> --name <aks-cluster-name> --output table














The output of this command looks like this :



large?v=v2&px=999.png



Also, not good enough, you were tasked with getting the VM size and the current amount.

By unleashing JMESPath with Azure CLI, you can achieve it with one command.



To succesfully complete our mission, the command is :













az aks show --resource-group <resource-group-name> --name <aks-cluster-name> --query "agentPoolProfiles[].{NodeCount:count, NodeVMSize:vmSize}" --output table













Output now seems much more useful:

medium?v=v2&px=400.png



Let's deconstruct the command above :



  • agentPoolProfiles[]: This fetches all items in the agentPoolProfiles array from the AKS cluster’s details.
  • {NodeCount:count, NodeVMSize:vmSize}: For each item in the agentPoolProfiles array, this creates a new object with two properties: NodeCount and NodeVMSize. The values for these properties are taken from the count and vmSize properties of each item in the agentPoolProfiles array.

Our outcome is far more usable and ready-to-use than the table one.



Tips on how to master JMESPath with Azure CLI


Here are some general tips to elevate your JMESPath game :




  • Properties: To get a property (like name), just use its name: name.


  • Nested Properties: If a property is inside another property (like address.city), use a dot: address.city.


  • Arrays: If a property is a list of items (like friends), use [] to get all items: friends[].


  • Specific Items in Arrays: To get a specific item from a list, use its position (like friends[0] for the first friend).


  • Filters: To find items in a list that meet certain conditions (like friends named ‘John’), use ?: friends[?name=='John'].


  • Functions: To do something special with the data (like count the number of friends), use functions: length(friends).


  • Projections: To apply an expression to each item in an array, use [] like property1[].property2.

  • Learn through Practice: use Azure CLI and --query daily to increase your knowledge by using.



Tips that save time and effort




Use parameter persistence



You're currently working on one single resource group and for the reminder of this session,

You do not wish to set --resource-group [name] or -g [name] every 2nd command.

Enter Parameter persistence. Turn this on by using :









az config param-persist on









After that, create your resource group :









az group create --name rg-name --location northeurope









For the remainder of this session, you can omit "--resource-group" from your Azure CLI commands.

For example :











az vm create -n vm-name --image Ubuntu2204 --size Standard_DS2_V2





Will create your VM, without the usage of --resource-group. Easy and time saving.

This will reset once you specify another resource group by the usage of --resource-group.



The command below will turn off parameter persistence after you've done :









az config param-persist off









Use the alias extension



The Alias extension let's you create your own aliases [similiar to the Bash ones] and persist them.

Install the Alias extension by using :







az extension add --name alias







For example, let's say I want to shorten the word "list" to the more command Linux friendly "ls" :







az alias create --name ls --command list







Now, instead of "az aks list", I can use :





az aks ls







You can also set your own parameters using a configuration file located at $HOME/.azure or %USERPROFILE%\.azure on Windows.



Prompt usage of this extension will let you use commands faster with less mistakes, and will make the commands easier for you to read.



Keep up with Azure CLI updates





Azure CLI release notes are right here.

This article holds all of the new information about Azure CLI and upcoming changes.



Conclusion



Azure CLI is a powerful tool that can be used to automate many of the tasks involved in managing your Azure resources.

It is also a great way to learn about the Azure platform and how it works.

It's super compelling and fun to use and will get you the data you need faster.

I encourage you to continue learning about the Azure CLI and how to use it to manage your Azure resources!

Continue reading...
 
Back
Top