Lead Image © nk88, photocase.com

Lead Image © nk88, photocase.com

Automating the Azure infrastructure using a command-line interface


Article from ADMIN 37/2017
When it comes to merging the cloud with local resources, Azure offers some useful automation tools.

Microsoft Azure [1] is increasingly merging with local infrastructures, whether for testing purposes or to allow for expansion of the production environment. For administrators, merging the cloud with local resources means great flexibility and many opportunities – but also the challenge of automating routine tasks to include cloud support. Fortunately, Microsoft has thought about the administrator's daily worries and has provided some useful automation tools.

Although the Azure environment includes GUI-based portals (see the "Choose your Portal" box), if you're serious about automation, you'll eventually want to integrate the cloud environment with custom scripts, which will require some knowledge of Azure's command-line interfaces (CLIs).

Choose your Portal

Currently Microsoft supports two portals for accessing Azure resources: the older, classic deployment portal, also known as the Service Management portal or Azure Classic Portal , and the model available since 2014 for the Resource Manager, long referred to as the "Preview Portal" or Azure Portal .

Not all resources or services created with one portal are available in the other. The two portals are based on different APIs and thus offer little mutual compatibility. An overview of which services work in which environment is available on Microsoft TechNet [2].

After a two-year trial period, Microsoft officially released the Preview Portal from its test phase in December last year and is now advertising its use. Fewer mouse clicks and more automation simplify the administrator's decisions. Administrators who need a complex setup (that possibly even repeats from time to time because it is a test environment) have a powerful option in the form of ARM (Azure Resource Manager) templates, which lets you set up a complete environment using a parameter file and a few mouse clicks.

Microsoft mostly uses Windows PowerShell for its automation examples, but the Azure environment also provides a command-line interface that will work on Windows, Mac OS, and Linux systems.

At the Command Line

The CLI plays a focal role in automating tasks in Azure. Microsoft offers installation packages for Windows, OS X, and Linux.

In this case, I will try out the Azure CLI using a Mac. After you have downloaded the installation package from the Azure Portal [3], the intuitive setup is performed straight away – assuming you have the rights to manage the Mac. In the Terminal window, first open a connection to your Azure subscription.

You'll need to provide your credentials. Enter the azure download command, which opens the browser of your choice to save a file with the logon data from Azure, along with a management certificate. Store this file in a safe place or preferably delete it, after you are done importing it with the following command:

azure account import filename

An alternative way to provide your credentials is to log in interactively to Azure. For more information, see the Azure documentation center [4].

For an overview of commands, type azure help (Figure 1). The list of commands depends on which deployment model you are using. When you display the list of commands, you will see which model is currently is use (Current Mode). If you are using the traditional deployment model (Service Management), you can switch to the Resource Manager with the azure config set mode arm command. If you output the list of commands again, it now looks very different.

Figure 1: Type azure help for information on azure commands.

Activities always begin with the azure keyword, followed by the command and the option. The command structure is always the same, and administrators will quickly become accustomed to it. In case of questions on syntax, the -h parameter for each command provides details. For example, if you want to know what options the command for managing virtual machines (VMs) offers, the command azure vm-h will help. The following command

azure vm create -h

gives you more information on the procedure for creating a VM.

Resource groups are very helpful for management and can be created quickly using the CLI. You can collectively perform administrative activities for a group, such as shutting down all the computers in the group. If the resource group is deleted later on, all the objects in the group conveniently disappear. If you create multiple VMs and do not specify a resource group, the runtime might create a separate group for each VM, which makes it difficult to manage the servers later on.

Creating New Objects

I am still at the command line on the Mac. Use the following command to create a new resource group:

> azure group create ita-resgroup westeurope

Note that this command (Figure 2) is based on the Resource Manager model. If the Terminal responds to the command with a statement that group is not an azure command, you may not be using the Resource Manager mode and might need to change.

Figure 2: Creating a resource group.

There are several ways to create virtual servers. For example, azure vm quick-create is useful for quickly creating a computer. The complete command looks like the following:

> azure vm quick-create -g ita-resgroup -n ita-server2016 -l westeurope -Q microsoftwindowsserver:WindowsServer:2016-Technical-Preview-with-Containers:2016.0.20151118 -y Windows -u klaus -p Passw0rd!

No matter how you create a server in Azure, with one of the CLIs, the PowerShell, or in the portal, you must always specify an underlying image. The following command generates a list of all images by the publisher MicrosoftWindowsServer:

> azure vm image list westeurope microsoftwindowsserver

Use the clipboard to take the name of the image from the Urn column, and paste it in azure vm quick-create after the -Q parameter. For a more specific setup command, use azure vm create, which offers more parameters.

Using Azure PowerShell

The CLIs are great for managing Azure services on Linux or Mac platforms. If you work exclusively on a Windows PC, PowerShell will be your interface of choice. You can download the Azure PowerShell package from the Azure website [5], and the installation is easy. Newcomers (and more advanced users) will find the reference in the documentation center a useful resource for tips on using cmdlets.

The cmdlets for the resource manager come with their own modules. Switching between the traditional deployment model and the resource manager model is no longer necessary in Azure PowerShell version 1.0, because separate cmdlets exist for activities related to the resource manager. These commands use the notation "Verb-AzureRm." If you want to shut down the server you just created on the Mac, for example, the cmdlet is as follows:

> Stop-AzureRmVM -ResourceGroupName "ita-resgroup" -Name "ita-srv2016" -force

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs

Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>


		<div class=