Question

Script to Shutdown All VMs with Nutanix-Clone

  • 2 July 2020
  • 4 replies
  • 3933 views

Badge

Hello,

Very new to the Nutanix CLI and I am doing a lot of PD testing to a DR Cluster and restoring them with the default prefix.

I was wandering if someone could help me with a script to shitdown all VM’s with the name starting ‘Nutanix-Clone-”.

I found this :

for vm_name in `acli vm.list power_state=on | grep -v ^'VM name' | awk '{print $1}'`; do acli vm.shutdown $vm_name; done

which is excellent but Im not sure how to further filter it by VM name?

Any help appreciated.

Note: Id also appreciate any help with a script that then deletes any VM and associated snapshot starting with ‘Nutanix-Clone-’ - doing 80 or so through the web console is hard work.

Thanks for you time,

Aaron

 

 


This topic has been closed for comments

4 replies

Userlevel 6
Badge +5

Hey @arboundy ,  about the script give me some time I will try it out in one of the test clusters and get back to you with the result. In the meantime here is an Acropolis cmdline guide to perform actions on a VM. The above script also uses these commands to perform the actions on the VMs:

https://portal.nutanix.com/page/documents/details/?targetId=Command-Ref-AOS-v5_17%3Aacl-acli-vm-auto-r.html

 

In this script you can specify the VM names here “grep -v ^'VM name' “, but with a different wildcard i.e ‘-v’ here is for invert matching i.e here which does not matches the given string specified in “VM name”. So this script is shutting down all the VMs other than the VMs with name ‘VM name’ specified in the argument. 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.

for vm_name in `acli vm.list power_state=on | grep -v ^'VM name' | awk '{print $1}'`; do acli vm.shutdown $vm_name; done


Here is a good guide to using grep in Linux:https://www.geeksforgeeks.org/grep-command-in-unixlinux/

In your scenario what you can do is use 

grep -i ^’Nutanix-Clone-’

 

Give me some time I will experiment this in one of my lab clusters and get back to you. In the mean time you can try it out too using the Acropolis guide and the script.

 

Let me know if you need anything else. :smile:

Badge

Thanks @AnishWalia20 - as I am raw (literally a couple of days) I look forward to your response and sincerely appreciate your assistance.

Userlevel 6
Badge +5

Hey @arboundy so I tried out and experimented with the scripts and here is how you can:

 

  1. Shutdown all VM’s with the name starting ‘Nutanix-Clone-” : Login to one of the CVMs on the cluster and run this script:

    cvm$ for vm_name in `acli vm.list power_state=on | grep -i ^'Nutanix-Clone-' | awk '{print $1}'`; do acli vm.force_off $vm_name; done
    Output of the above
    This will power off all the VMs starting with name “Nutanix-Clone-”. 

     
  2. Deletes any VM starting with ‘Nutanix-Clone’:
    cvm$ for vm_name in `acli vm.list power_state=off | grep -i ^'Nutanix-Clone-' | awk '{print $1}'` ; do acli vm.delete $vm_name delete_snapshots="true" ; done
    Output of the above


    In the above scenario the VMs Starting with name ‘Nutanix-Clone-’ which were in powered-off state along with their snapshots were deleted. The option “delete_snapshots=”true” deleted the snapshots associated with the VMs.

The only thing you will have to do is enter “yes” for each VM as there will be a prompt for confirmation to delete.:grin:

 

Let me know if you need help with anything else as I would be more that happy to help.:smiley:

Userlevel 6
Badge +5

Hey there @arboundy , just following up, were you able to execute the above script and achieve the above?

 

Let me know if you had any other doubts, I would be more than happy to clear them.:smile: