Managing Azure Workloads with Azure CLI

There are multiple ways to manage your workloads on Microsoft Azure, and some of them are very intuitive like the GUI web-based interface or the Azure PowerShell module for PowerShell aficionados. But one tool among all of the available ones that attracted my attention is Azure CLI.

Azure CLI is a cross-platform command line tool that can be installed on any OS (MacOS, Windows, or Linux) and is as powerful as impressive, when using it to automate all kinds of things in the Azure Cloud infrastructure.

So If you’re managing Cloud workloads from your macOS or Linux station, this great tool will bring more fun to your automation journey in Azure.

At the time of this writing, it is on version 2.0. In this post, we will install Azure CLI on a CentOS 7 box and explore some basic features by creating an Azure resource group and spinning-off a VM inside it. Let’s get started.

Installing Azure CLI


Installation is straight forward. First we need to import the required repository key then go ahead with actual installation.

  • Importing the Microsoft repository key

$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
  • Creating local azure-cli repository information

$ sudo sh -c 'echo -e "[azure-cli]\nname=Azure CLI\nbaseurl=https://packages.microsoft.com/yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo'
  • Installing Azure CLI

$ sudo yum install azure-cli
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.kw.zain.com
 * extras: centos.kw.zain.com
 * updates: centos.kw.zain.com
Resolving Dependencies
--> Running transaction check
---> Package azure-cli.x86_64 0:2.0.45-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================
 Package                                      Arch                                      Version                                         Repository                                    Size
===========================================================================================================================================================================================
Installing:
 azure-cli                                    x86_64                                    2.0.45-1.el7                                    azure-cli                                     22 M

Transaction Summary
===========================================================================================================================================================================================
Install  1 Package

Total download size: 22 M
Installed size: 147 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for azure-cli
azure-cli-2.0.45-1.el7.x86_64.rpm                                                                                                                                   |  22 MB  00:01:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : azure-cli-2.0.45-1.el7.x86_64                                                                                                                                           1/1
  Verifying  : azure-cli-2.0.45-1.el7.x86_64                                                                                                                                           1/1

Installed:
  azure-cli.x86_64 0:2.0.45-1.el7

Complete!
  • Checking the Azure CLI successful installation

You can simply type az to check if the installation went fine

$ az
No handlers could be found for logger "cli.azure.cli.core._session"

Welcome to Azure CLI!
---------------------
Use `az -h` to see available commands or go to https://aka.ms/cli.

Telemetry
---------
The Azure CLI collects usage data in order to improve your experience.
The data is anonymous and does not include commandline argument values.
The data is collected by Microsoft.

You can change your telemetry settings with `az configure`.


	 /\
	/  \    _____   _ _  ___ _
   / /\ \  |_  / | | | \'__/ _\
  / ____ \  / /| |_| | | |  __/
 /_/    \_\/___|\__,_|_|  \___|


Welcome to the cool new Azure CLI!

Use `az --version` to display the current version.
Here are the base commands:

	account           : Manage Azure subscription information.
	acr               : Manage Azure Container Registries for private registries within Azure.
	acs               : Manage Azure Container Services.
	ad                : Manage Azure Active Directory Graph entities needed for Role Based Access
					   Control.
	advisor           : Manage Azure Advisor.
	aks               : Manage Azure Kubernetes Services.
	ams               : Manage Azure Media Services resources.
	appservice        : Manage App Service plans.
	backup            : Manage Azure Backups.
	...

 

Connecting to Azure through Azure CLI


Once you’re sure your Azure CLI installation is successful, you first need to login to Azure with the az login command.

$ az login

As soon as you run this command, a browser window will prompt pointing to the Azure Web Portal and inviting you to enter your Azure credential.

$ az login
Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
START /usr/bin/firefox "https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&redirect_uri=http://localhost:8400&state=code&resource=https://management.core.windows.net/&prompt=select_account"

Type your Azure username and password and once the authentication is done, the page be will redirected to the Azure CLI documentation.

If you have any issue with your browser display, disable temporary selinux on your host.

$ sudo setenforce 0
$ getenforce
Permissive

If the is authentication process is successful, you will have an output similar to this.

$ az login
Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
START /usr/bin/firefox "https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&redirect_uri=http://localhost:8400&state=code&resource=https://management.core.windows.net/&prompt=select_account"
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "id": "97e4b905-5dfe-4b42-90ad-8231af0b9c6b",
    "isDefault": true,
    "name": "Plateformes MSDN",
    "state": "Enabled",
    "tenantId": "b5b6c068-4ffd-4a61-ab9a-26f8c311b19a",
    "user": {
      "name": "<AzureUserName>",
      "type": "user"
    }
  }
]

As an alternative to the Web Portal authentication, you can also use the following command for a CLI based authentication, where <username> is your Azure username.

$ read -sp "Azure password: " AZ_PASS && echo && az login -u <username> -p $AZ_PASS
Azure password:

 

Creating Azure Resources with Azure CLI


  • Creating a Resource Group

Here is an overview of the current Resource Groups in the Azure Dashboard.

Let’s start by creating a Resource Group named testRG in West Europe region.

$ az group create --name testRG --location "West Europe"

{
  "id": "/subscriptions/97e4b905-5dfe-4b42-90ad-8231af0b9c6b/resourceGroups/testRG",
  "location": "westeurope",
  "managedBy": null,
  "name": "testRG",
  "properties": {
	"provisioningState": "Succeeded"
  },
  "tags": null
}

Resource Group created! And we can confirm that testRG is there from the Azure Web Console.

  • Listing existing available images

To list all available images that can be used for VM creation, use below command.

$ az vm image list

You are viewing an offline list of images, use --all to retrieve an up-to-date list
[
  {
	"offer": "WindowsServer",
	"publisher": "MicrosoftWindowsServer",
	"sku": "2016-Datacenter",
	"urn": "MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest",
	"urnAlias": "Win2016Datacenter",
	"version": "latest"
  },
  {
	"offer": "WindowsServer",
	"publisher": "MicrosoftWindowsServer",
	"sku": "2012-R2-Datacenter",
	"urn": "MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest",
	"urnAlias": "Win2012R2Datacenter",
	"version": "latest"
  },
  {
	"offer": "WindowsServer",
	"publisher": "MicrosoftWindowsServer",
	"sku": "2008-R2-SP1",
	"urn": "MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest",
	"urnAlias": "Win2008R2SP1",
	"version": "latest"
  },
...
 {
	"offer": "CentOS",
	"publisher": "OpenLogic",
	"sku": "7.3",
	"urn": "OpenLogic:CentOS:7.3:latest",
	"urnAlias": "CentOS",
	"version": "latest"
  },
  {
	"offer": "openSUSE-Leap",
	"publisher": "SUSE",
	"sku": "42.3",
	"urn": "SUSE:openSUSE-Leap:42.3:latest",
	"urnAlias": "openSUSE-Leap",
	"version": "latest"
  },
  {
	"offer": "RHEL",
	"publisher": "RedHat",
	"sku": "7.3",
	"urn": "RedHat:RHEL:7.3:latest",
	"urnAlias": "RHEL",
	"version": "latest"
  },
  • Launching VMs on Azure VMs

Time now to spin-off a VM inside the testRG created previously from a Windows Server 2016 Datacenter image. I’m going for a smaller VM size with Basic_A1.

$ az vm create -n WinSRV -g testRG --image win2016datacenter --size Basic_A1 --admin-username azureuser

Admin Password:
Confirm Admin Password:
 - Running ..

In less than 30 seconds, my VM was already running and accessible from the Azure Portal.

This was really a quick overview of what Azure CLI is and what it can bring to the table for your Azure daily administration. You can now head over the official documentation to get a better idea of its features and explore some detailed and great examples for both Windows and Linux VMs related to this tool.

Leave a Comment

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