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 usingaz login:
Then, lookup your most used subscription ID and set it as default, using:
az account set -s $subid
Runaz configureto get Azure CLI's current settings, we'll tweak them a bit:
You'll encounter the below output:
Let's have a quick run through.
Cloud Namemeans the current Azure Cloud that you're using. This does not need to be changed unless you're using Azure Government.
defaultsneed 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 useaz config setto change this by exiting theaz configurecommand in a key/value pair. Let's try changing our default location to West Europe:
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 theaz configurecommand, 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.
coregives you the ability to change Azure CLI's behavior. A good example will be making Azure CLI default to yes on every prompt by addingdisable_confirm_prompt=Yeson 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:
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,yamlcandtable.
JSONis the default output.
JSONCis an interesting one. It highlightsvaluesinJSONformat and make them distinct from Dictionary and Array keys:
If you like JSON, this will defintely make your work easier.
YAMLmakes the output return in a yaml format.
YAMLCis the same as JSONC, but for yaml. Using the same command again:
TSVwill make the output return in a tsv format.
tableis the one to use if you need condensed information. Using the same command again:
Important Note: You can alwaysbypassthe default configuration for output using-otrigger. for example,az group list -o tablewill 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. Enablingthis is recommended for debugging purposes.
This setting will log the exit code of your commands to%HOME%/.azure/commandsfolder on Linux and%USERPROFILE/.azure/commandson Windows :
If you're running a complex command and you want detailed output,--debugis 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--debugto see everything behind the scenes and then if something fails troubleshooting will be easier:
az aro create -n name -g resourcegroup --debug
Use--no-waitfor long operations
Leverage the--no-waittrigger 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-waitand 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--yesfor operations that need user prompt
If you're sure about an operation that requires user prompt, use--yesto bypass Azure CLI's prompt.
Back to theaz aks deletecommand, adding--yeswill remove the prompt asking us to confirm:
az aks delete -n clustername -g resourcegroup --no-wait --yes
Useaz interactiveif you're new to Azure CLI
az interactiveis 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 :
In the next article we'll explore more ways to effectively work with Azure CLI.