Press "Enter" to skip to content

Deallocating multiple VMs with azure-cli & bash

The great thing about Azure (and to that extent Azure Stack) is that you can quickly spin up and down test environments. If you’re like me, you prefer a command line over any browser interface any day of the week for repeatable tasks.
In the Azure world, the great azure-cli command line tool comes to the rescue. The great thing about it is that it is a) open source, and b) cross platform. So working with resources in Azure from my Linux workstation is no problem. Until I ran into one.
For testing out some network configurations, I spun up a bunch of VMs in Azure. Once I was done, I wanted to deallocate them to not have them consume resources, and thus cost money. That’s after all the great thing about a cloud.
All my VMs were in their own resource group to have a dedicated network for them and since it’s a test lab, easy to manage without interferring with other resources. The azure-cli provides the functionality on a per VM basis to deallocate a VM from the shell. That is all good for one, but not for multiples. Let’s take a look:

mschulz@eowyn:~$ azure vm list -g db2lab
 info:    Executing command vm list
 + Getting virtual machines
 data:    ResourceGroupName  Name       ProvisioningState  PowerState      Location  Size
 data:    -----------------  ---------  -----------------  --------------  --------  ----
 data:    db2lab             db2-node1  Succeeded          VM running       westus    Standard_A2
 data:    db2lab             db2-node2  Succeeded          VM running       westus    Standard_A2
 info:    vm list command OK
 This is the environment for the simple test. Two VMs (more to be added) to form a cluster in resource group db2lab.

The azure-cli tool provides us the name of the resource group, as well as the VM names.
From here we can do a simple one liner to get the respective VM names we want to target and write
them into an array for later processing:

mschulz@eowyn:~$ vmname=($(azure vm list |grep -i db2lab | awk '{print $3}'))
 mschulz@eowyn:~$ echo ${vmname[@]}
 db2-node1 db2-node2
 Now that we have those names in the array, we can then proceed to process them in a simple loop that
 will take the action on the VMs we want.
for i in "${vmname[@]}"
 do :
 azure vm deallocate –g db2lab -n $i

The above example reads the values from the array, passes them to the azure-cli and deallocates the VMs. In order to do the revers, we could simply replace deallocate with start.
Since this would be a bit cumbersome to do every time we need this operation, let’s pull all this together in a handy shell script:

startvms() {
         vmname=($(azure vm list |grep -i $rgroup | awk '{print $3}'))
         for i in "${vmname[@]}"
 do :
 azure vm start -g $rgroup -n $i
stopvms() {
         vmname=($(azure vm list |grep -i $rgroup | awk '{print $3}'))
         for i in "${vmname[@]}"
 do :
 azure vm stop -g $rgroup -n $i
deallocvms() {
         vmname=($(azure vm list |grep -i $rgroup | awk '{print $3}'))
         for i in "${vmname[@]}"
 do :
 azure vm deallocate -g $rgroup -n $i
case $2 in
 start)  startvms ;;
 stop)   stopvms ;;
 deallocate)     deallocvms ;;
 *)      echo "usage: $0 "resource group" start|stop|deallocate" >&2
 exit 1

Using this script, we can now on the command line specifiy the resource group in question, plus
define the action we want to be taken (start, stop, or deallocate).

The azure-cli tool is a fantastic resource in the tool belt of the Azure admin to perform many tasks from the command line of either Linux, WIndows, or Mac OS X. Look Ma’, no GUI.


Be First to Comment

Leave a Reply

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