Manage multiple UVMs at once using custom aCLI scripts on a Nutanix cluster

  • 14 July 2020
  • 0 replies

Userlevel 5
Badge +4

There would be scenarios where in a system administrator or an engineer managing a Nutanix cluster would want to say:


Perform some action like shutdown, power-on, delete, take a snapshot, clone etc ib bulk or on multiple VMs at once with some matching critearia such as say common Starting names, or common ending names, some pattern in names and all the powered-on UVMs etc.


One way is to one-by-one go and perform that action from Prism for each of those VMs which is very tedious in scenarios where say we have 20 VMs etc on which we want to perform same actions say like shutting them down.


What can we do ?


In such scenarios we can use some basic shell scripting along with aCLI(Acropolis command line tool).

So aCLI is a cmdline tool for managing hosts, networks snapshots, UVMs etc on a Nutanix cluster. Here is a document which explains how to use aCLI


So below are some examples of such scenarios where we would want to execute some actions on multiple VMs say with similar names etc using shell scripting and acli commands. These are pretty easy and quite handy in some scenarios and would save you a lot of time.


Scenario 1) Shutdown all UVMs which have names starting with say ‘Nutanix-’.


To achieve the above you can login to one of the CVMs in the cluster and execute the below command:


cvm$ for vm_name in `acli vm.list power_state=on | grep -i ^'Nutanix-' | awk '{print $1}'`; do acli vm.force_off $vm_name; done


So this script is shutting down all the VMs starting with name ‘Nutanix-’ specified in the argument with

grep -i


The grep filter searches the output of command ‘acli vm.list power_state=on’ above which lists all the powered on user VMs for a particular pattern of names starting with ‘Nutanix-’ . Also the ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

Now the for loop runs/iterates for number of times we observe a VM which is powered on and starts with name ‘Nutanix-’ . Also the looping variable ‘vm_name’ stores the name of those UVMs matching the above criteria. 

do acli vm.force_off $vm_name


The above defines what action we want to perform on those VMs. In this case we are shutting down all those VMs. The aCLI command ‘acli vm.force_off $vm_name’ powers off those VMs whose names start with ‘Nutanix-’. The VM names are accessed from the looping variable vm_name using $vm_name .

To know more about the aCLI VM operations you can go through aCLI VM ops .

And its that simple and no rocket-science. So it is all divided into 3 parts, run a for loop and filter some results using grep and then perform some actions in the do section of for loop.


Scenario 2) Power-on all UVMs which have names starting with ‘Nutanix-’ :


Now all we need to do is perform a different action and use the correct aCLI command.

cvm$ for vm_name in `acli vm.list power_state=off | grep -i ^'Nutanix-' | awk '{print $1}'`; do acli vm.on $vm_name; done


So we just list all the VMs which are powered off and search for all VMs starting with name ‘Nutanix-’ and power them on.


Scenario 3) Delete all VMs and it’s associated snapshots whose name ends with ‘Clone’.


cvm$ for vm_name in `acli vm.list power_state=off | grep -i $'Clone' | awk '{print $1}'` ; do acli vm.delete $vm_name delete_snapshots="true" ; done


Here we have used $  wildcard with grep to search for the names in the output which ends with name ‘Clone’.

In the above command “acli vm.delete $vm_name delete_snapshots="true" deleted all the VMs whose names ended with ‘Clone’ and all their associated snapshots.


NOTE: A word of caution, please use the above scripts wisely and cautiously.


So the above were some basic examples of how you can perform such actions like power-on, power-off, delete etc actions in bulk for multiple VMs matching some criteria such as starting with common name, ending with specific names etc.


Now the above can be tweaked for different use cases. All you need is some basic knowledge of shell scripting, specifically using for loops, using grep wisely to filter the results and it’s done. Grep can be used in lots of more ways to filter results and search for patterns in a file or output. 


I would insist the readers to go through some basics of Using for Loops in Shell Scripting and using Grep to filter/search for patterns .

This article also explains really well about Grep


So go ahead and learn something new which could help you and save a lot of your time. I hope this post can save your valuable time and help manage your cluster more easily.

0 replies

Be the first to reply!