Today we will see how easy Microsoft has made it to install azure CLI which is a Command Line Interface (CLI) tool that manages azure Cloud resources. In previous posts I explored the installation for the CLI software of both Oracle and AWS, I have to say that Azure CLI is by far the simplest to configure. As usual, this post will be followed by at least 2 more articles that will cover two ways of provisioning an azure vm (AZ CLI, terraform).
Requirement
Whether on windows or on Linux the basic installation pf Azure CLI will always require 2 elements:
- Azure Free Tier account
- Python 3.6.x,3.7.x or 3.8.x
I. Azure CLI Installation
Windows
1- Download and execute the following Az-Cli MSI installer(current version: 2.14.0) https://aka.ms/installazurecliwindows
2- Follow the on-screen instructions (the MSI installer is also used to upgrade existing installations) .
3- Run the version command to confirm that Azure CLI was installed correctly.
C:\Users\kosse> az --version azure-cli 2.14.0 core 2.14.0 telemetry 1.0.6 Python location 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe' Extensions directory 'C:\Users\kosse\.azure\cliextensions' Python (Windows) 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] ... Your CLI is up-to-date.
C:\Users\kosse> where az > C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az Note:
az
command can run from either Windows Command Prompt or PowerShell. - Linux
There is either an all-in-one install command or a multi step and interactive mode install. I’ll focus on the simpler approach.
► Ubuntu
brokedba~$ curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
brokedba~$ az --version
azure-cli 2.14.0
core 2.14.0 telemetry 1.0.6Python location '/opt/az/bin/python3' Extensions directory '/home/brokedba/.azure/cliextensions'
brokdba:~$ which az
/usr/bin/az
► REDHAT, Fedora, CENTOS, OLinux
# RHEL/OL/CENTOS (7,8+), Fedora 24+
# -- Import Microsoft Repo Key
[r@localhost]# sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
# -- Add Az Cli repo
[r@localhost]# sudo sh -c 'echo -e "[azure-cli] name=Azure CLI baseurl=https://packages.microsoft.com/yumrepos/azure-cli enabled=1 gpgcheck=1 gpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'
# Install rpm package
[r@localhost]# sudo yum install azure-cli
► REDHAT install including Python
##RHEL bundle install with python 3
curl -sL https://azurecliprod.blob.core.windows.net/rhel7_6_install.sh | sudo bash
II. Sign in to Azure from AZ CLI
Once your Azure free Tier account is created and azure cli installed, you won’t need to gather any API key or other usually required credentials (as in OCI,AWS). All you need is run the below az login command, this will open a browser so you can sign in with your registered Microsoft account (additional login options are available here ).
brokedba~$ az login
- Once signed in, your web page will confirm that az cli is now configured accordingly .
- The above steps are common across platform no matter on which OS you installed your Az cli
- Now we can run az account command to verify the our new configuration
$ az account list --- OR account show { "environmentName": "AzureCloud", "homeTenantId": "6187922f-3371-xxxxx-xxxx-xxxxxxxxxxxx", "id": "4dc6d66a-f5c9-4f0e-8f01-8b46e413ea93", "isDefault": true, "managedByTenants": [], "name": "BrokeDBA", "state": "Enabled", "tenantId": "6187922f-3371-xxxx-xxxx-xxxxxxxxxxxx", --> same as homeTenantId "user": { "name": "xxxxxx@outlook.com", "type": "user" } }
- That’s it , you are now ready to go with azure cli (for 30 days).
- Using az configure you can configure basic or more complex parameters for az cli which will be stored in the below file
$ cat ~/.azure/config
[cloud]
name = AzureCloud
[core]
output = table --- possible values : json|jsonc|yaml|yamlc|tsv|table
collect_telemetry = no
cache_ttl = 10
[logging]
enable_log_file = no
III.Test your first API request
Few notions worth reminding before hitting the terminal with your favorite azure-cli requests :
A. Command structure : is based on the below components
$az [group] [subgroup] [command][
parameters
]
Parameters:
Will be followed by their values, this is resource bound so it will always depend on the group & subgroup value in the
command.The value type can also vary (string, integer, JSON, list,…)
- Result related parameters :
1- “--output[-o]” : will format Azure CLI output into Json(colored), yaml(colored), Table, or tsv (raw).
2- “--query” : Allows to pick the list of fields to return in the response. It can be used to do some filtering.
B. Query :
The --query option relies on JMSPath and its filtering is done at client side. The best way to learn is to fetch the output in Json then locate the node where your information resides.
- To demonstrate the principle, here’s an example based on the account info displayed earlier in this article
$ az account list -o json ----> see original query further above
... # >> Display the user’s email
$ az account list --query "[0].user.name" -o json
"brokedba@outlook.com"
# Table output
$ az account list --query "[0].user.{email:name}" -o table
--------------------
kosseila@outlook.com
Examples
There are few requests that you can run to practice with azure-cli. Below commands are good examples to start with.
$ az account list-locations --query "[].{DisplayName:displayName, Name:name}" -o table
DisplayName Name ------------------- ------------------ East Asia eastasia Southeast Asia southeastasia Central US centralus East US eastus East US 2 eastus2 West US westus North Central US northcentralus South Central US southcentralus North Europe northeurope West Europe westeurope Japan West japanwest Japan East japaneast ...
$ az vm list --resource-group brokedba --show-details --query "[*].{VM:name, Size:hardwareProfile.vmSize, OS:storageProfile.osDisk.osType, State:powerState}" --output table
+-----------------------+-------------------+---------+-------------+ | VM | Size | OS | State | +-----------------------+-------------------+---------+-------------+ | BrokeVM | Standard_B1ls | Linux | VM running | | demoinstance | Standard_D2s_v3 | Linux | VM running | +-----------------------+-------------------+---------+-------------+
$ az storage blob list -c mycontainer
$ az group create -l "eastus" -n brokedba
$ az vm create --resource-group brokedba --name brokeVM --size Standard_B1ls --image UbuntuLTS --generate-ssh-keys
----------------------------------------- | DescribeInstances | +----------+----------------------------+ | AZ | us-east-1a | | PrivIP | 192.168.10.45 | | Rootdev | /dev/sda1 | | Subnet | subnet-08b49f9682c5da2b6 | | VPCID | vpc-096b461ebe9d06ff3 | | image | ami-01861c2f0a2adfdb7 | +----------+----------------------------+
$ az find "az vm"
Finding examples...
Get the details of a VM. (autogenerated)
az vm show --name MyVm --resource-group MyResourceGroup
Aliases
-
Unlike in AWS CLI Azure leaves the choice to the users to create their own aliases using the alias extension (no predefined aliases). This could help shorten common queries like listing resource groups/running vms etc. You can quickly install it by running the below commands:
$ az extension add --name alias
$ az extension list --output table
Experimental Exten-Type Name Path Preview Version
------------- ----------- ------ ------------------------------- --------- -------
False whl alias ~/.azure/cliextensions/alias True 0.5.2
$ az alias create --name rg --command group
$ az alias list
Alias Command
----- ---------
rg group
Escape characters
- Escape characters in az-cli query blocks can be different depending on the OS
In Linux :
- Backslash and backward single quotes –> \`value\` if the query block is surrounded by double quotes (--query "..")
- Backward single quotes only –> `value` when the query block is surrounded by single quotes (--query '..')
In Windows
- Backward single quotes only –> `value` but the query block must be surrounded by double quotes (--query "..")
Example :
List vm sizes in eastus region with 4vcpus of type D (general compute) or E (memory optimized compute) sorted by RAM.
LINUX
$ az vm list-sizes -l eastus --query "sort_by(@,&memoryInMb)[?numberOfCores == \`4\` && (contains(name,\`_E4\`) || contains(name,\`_D4\`))]"
OR$ az vm list-sizes -l eastus –query 'sort_by(@,&memoryInMb)[?numberOfCores == `4` && (contains(name,`_E4`) || contains(name,`_D4`))]'
Name NumberOfCores DiskSizeInMb ResceDiskSizeInM MemoryInMb MaxDataDisk
-------------- ------------- -------------- ------------------ ----------- -----------
Standard_D4s_v3 4 8 16384 1047552 32768
Standard_D4_v3 4 8 16384 1047552 102400
...
Standard_E4_v3 4 8 32768 1047552 102400
WINDOWS
$ az vm list-sizes -l eastus --query "sort_by(@,&memoryInMb)[?numberOfCores == `4` && (contains(name,`_E4`) || contains(name,`_D4`))]"
Conclusion:
In this tutorial we learned how to install and configure azure-cli which took 5 minutes. We also described the command syntax and tried few requests using az-cli.
Finally, feel free to consult Azure CLI Command Reference for more details and examples on az-cli requests.
Cheers.