Take your Azure CLI skills to the next level
Published Jan 24 2023 04:56 AM 2,010 Views
Microsoft

We'll start by installing the Azure CLI command [or opening an Azure Cloud Shell].

 

After you've installed Azure CLI, set your default account using az login:

az login

Then, lookup your most used subscription ID and set it as default, using:

az account set -s $subid

Run az configure to get Azure CLI's current settings, we'll tweak them a bit:

az configure

 

You'll encounter the below output:

1.png

 

Let's have a quick run through.

 

Cloud Name means the current Azure Cloud that you're using. This does not need to be changed unless you're using Azure Government.

 

defaults need some more elaboration.

As you can see, my default location is set to East US. This means if I don't set the -l trigger at resource creation,

 

Azure CLI will automagically place them in East US. I can use az config set to change this by exiting the az configure command in a key/value pair. Let's try changing our default location to West Europe:

2.png

 

As you can see, my default location changed to West Europe. We can set other default configuration options such as: Resource Groups, App Service, VM name, VMSS name, Azure Container Registry and more.

 

Important Note: If you don't like the az configure command, you can write the configuration yourself by editing the Azure CLI config file which exists at : $HOME/.azure/config on Linux and MacOS and %USERPROFILE%/azure if you're using Windows.

 

core gives you the ability to change Azure CLI's behavior. A good example will be making Azure CLI default to yes on every prompt by adding disable_confirm_prompt=Yes on the Azure CLI configuration file, however, this is not a recommended approach ,so, use this for development purposes only.

 

 

Styling and Customization

Azure CLI includes a lot of styling options for having a clean and understandable output. Back to the az configure command, let's type Y and continue making changes, Azure CLI will now prompt you for a default output:

3.png

 

By default if not set differently Azure CLI will always return the output as JSON.

Let's look at the following options - json,jsonc,tsv,yaml,yamlc and table.

 

JSON is the default output. 

JSONC is an interesting one. It highlights values in JSON format and make them distinct from Dictionary and Array keys:

4.png

 

If you like JSON, this will defintely make your work easier. 

YAML makes the output return in a yaml format. 

YAMLC is the same as JSONC, but for yaml. Using the same command again:

 

5.png

 

TSV will make the output return in a tsv format. 

table is the one to use if you need condensed information. Using the same command again:

 

6.png

 

Important Note : You can always bypass the default configuration for output using -o trigger. for example, az group list -o table will always return the output in a table format, regardless of what is set by default.

 

Choose the one that you like the most, and let's move on.

 

Next, Azure CLI will ask you if you want to log to file. Enabling this is recommended for debugging purposes.

This setting will log the exit code of your commands to %HOME%/.azure/commands folder on Linux and %USERPROFILE/.azure/commands on Windows :

7.png

 

 

General Tips

 

  • Use --debug trigger

If you're running a complex command and you want detailed output, --debug is your friend.

Use this trigger whenever you want to see how ARM handles your request.

 

Let's say we're running a complex creation of Azure Red Hat OpenShift. The basic command is -

az aro create -n name -g resourcegroup

This command will not show a lot of information back to the user on a failure.

Add --debug to see everything behind the scenes and then if something fails troubleshooting will be easier:

 

az aro create -n name -g resourcegroup --debug

 

  • Use --no-wait for long operations

Leverage the --no-wait trigger if your command takes time to complete and you need to run other commands meanwhile.

For example, let's say we're deleting an Azure Kubernetes Service (AKS) cluster :

az aks delete -n clustername -g resourcegroup

This command will prompt us to make sure that we wish to accomplish this operation and then will make us wait for it to be completed.

 

Add --no-wait and reclaim your terminal while the command continues to run in the background:

az aks delete -n clustername -g resourcegroup --no-wait

Important Note: --no-wait trigger will not return any output back, so you need make sure to track the result of your operation.

 

  • Use --yes for operations that need user prompt

If you're sure about an operation that requires user prompt, use --yes to bypass Azure CLI's prompt.

 

Back to the az aks delete command, adding --yes will remove the prompt asking us to confirm:

 

az aks delete -n clustername -g resourcegroup --no-wait --yes

 

  • Use az interactive if you're new to Azure CLI

az interactive is a fantastic way to learn Azure CLI.

This will install another extension on your workstation and will help you write commands in a nice and clean way.

This adds auto-completion, command descriptions and more :

 

8.png

 

In the next article we'll explore more ways to effectively work with Azure CLI. 

3 Comments
Co-Authors
Version history
Last update:
‎Jan 24 2023 09:38 AM
Updated by: