Azure CLI 2.0 tips and tricks

Microsoft Azure has different ways to interact with ranging from the portal, powershell, rest api and SDK’s for various programming languages. But Microsoft also released a CLI first in node.js but in version 2.0 everything was rearchitect to python. Also version 2.0 only support ARM. The main goals are speed, cross compatibility and ease of use.

To get it running you have multiple options for multiple operating systems. For me on my mac there are 2 options:

  • Homebrew: Homebrew is a package manager for mac that let’s you install different open source software. Some love it, some hate it, it has it quirks. But for me it works great once installed.
  • Docker image: Just install docker and enjoy many containers with lots of different software including that has Azure CLI 2.0 ready to go.

But Microsoft has made it even easier if you don’t want or can’t install anything on your computer. Or you are at a customer site and need it quickly for a change. Azure Cloud Shell to the rescue! This is a recent service in the Azure Portal that will launch a container using a storage account and get’s you a linux shell ready to go. Start up time is around 30seconds! That beats any local installation. Azure Cloud Shell also contains other tools like Terraform, Ansible, …

To start it look for the cloud shell icon in the portal  or go to https://shell.azure.com/. Select a storage account so you can transfer files to it and you are ready to go. There is also a windows/powershell version available.

The portal over all cloud providers are all dense in information and find that information can be a challenge. Maybe you want to perform actions on multiple resources in Azure or repeat certain actions over and over. That is were Azure CLI is shines for me. Below some examples that I use frequently. Don’t forget to login to Azure using the command “az login”.

  • Create a resource group:
    • az group create --location "westeurope" --name "myresourcegroup"
  • Create a VNET with 1 subnet:
    • azure network vnet create --name "TEST-VNET"  --resource-group "myresourcegroup" --location "westeurope" --address-prefix 172.16.136.0/22 --subnet-name "TEST-SUBNET-NGF" --subnet-prefix 172.16.136.0/24
  • Create a virtual machine in a specific subnet:
    • az vm create --resource-group"myresourcegroup"" --name "TEST-VM-NGF" --location "westeurope" --nics "TEST-VM-NGF-NIC" --image "barracudanetworks:barracuda-ng-firewall:byol:7.2.015802" --admin-username azureuser --authentication-type password --admin-password "mypassword"
  • Delete one resource group:
    • az group delete --name myresourcegroup
  • Delete multiple resource groups:
    • for i in CLTEST2-RG-WEB CLTEST2-RG-SQL CLTEST2-RG-WAF CLTEST2-RG-NGF CLTEST2-RG-VNET; do echo "Deleting $i"; az group delete --name $i -y; done

Now in Powershell you can used objects to pass around more than one variable. In Azure CLI this is not possible. For this you can use JMESPATH which is a query language for JSON. At first I was a little bit lost but then I found this nice tool called jpterm or JMESPath-terminal and that helped a lot. It is also a python tools that you can install using pip:

$ pip install jmespath-terminal

Microsoft also has some nice information to get your started here.

My goal was to list all my VM’s in my Azure Subscription and the VM Size they were using. That way I could get an easy overview what limits we might have reached. To start and test the query you can feed your data directly into jpterm.

$ az vm list --output json | jpterm

Then you can construct your query using this curses based GUI, et voila…

The final query looked like this:

$ az vm list --query "[?location=='westeurope'].{name:name, location:location, vmsize:hardwareProfile.vmSize}" --output table
Name Location Vmsize
--------------- ---------- ---------------------
JVH11-VM-NGF westeurope Standard_F1s
JVH11VMWEB westeurope Standard_D1

An other example to list which vm’s support Availability Zones in your region:

$ az vm list-skus --location westeurope --query "[?resourceType=='virtualMachines'].{name:name, zones:join(', ',locationInfo[].zones[])}" --output table

What is your favourite Azure CLI 2.0 command? Let us know.

Recent Posts

jvhoof Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

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