Create/Remove snapshots for all virtual machines inside VM folder using vRO Plugin

Have you ever tried to create snapshots for multiple virtual machines? You can select many VM and power on/off, migrate or delete, but there is no option to create or remove snapshots. When you need to do it on few servers, it is not a problem, but what if you need to make 20 snapshots? A lot of clicking. Below I show you how to extend vCenter functionality using vRO Plugin and add context action to create or remove snapshots for all virtual machines inside a folder. You can easily modify provided workflows and create such action for resource pool or whatever you need. Have fun!

This part includes:

Requirements that are not covered by this post:

  • Integration of vRealize Orchestrator with vCenter Server (here).
  • Integration of vRealize Orchestrator with SMTP host (only for sending emails).

Part 1 – vRO Workflow that creates snapshots for all virtual machines inside a folder.

In this case, we will use actions that are delivered out of the box in vRO. The most significant disadvantage is that if some of the snapshot creation tasks fail, the workflow will fail and stop processing the rest of the VMs. But the workflow is simple. Sometimes that’s enough.

vRO-FolderSnap1

We need some inputs from the user/administrator:
vmFolder (VC:VmFolder) – It is our folder where VMs are.
name (string) – Name of snapshot.
description ( string) – Snapshot description.
memory (boolean) – If yes, include virtual machine’s memory.
quiesce (boolean) – If yes, quiesce guest file system(requires VM tools).

vRO-FolderSnap1a

Also we need to define some variables:
allVms (Array/VC:VirtualMachine) – List of all VMs inside folder. We will get it through vRO action.
task (VC:Task) – We will use it to check if task in vCenter Server is finished.
progress (boolean) – Monitor task progress.
pollRate (number) – Polling rate for the task state [seconds].

vRO-FolderSnap1b

The first element is an vRO action called: com.vmware.library.vc.folder/getAllVirtualMachinesByFolderIncludingSubFolders
Inputs:
vmFolder (VC:VmFolder)
Outputs:
allVms (Array/VC:VirtualMachine)

The second element is a scriptable task.
Inputs:
allVms (Array/VC:VirtualMachine)
description ( string)
name (string)
memory (boolean)
quiesce (boolean)
task (VC:Task)
progress (boolean)
pollRate (number)

for each (var vm in allVms)
{
    //Create snapshot for VM.
    task = System.getModule("com.vmware.library.vc.vm.snapshot").createSnapshot(vm,name,description,memory,quiesce);
    //Wait until task is completed.
    System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
    System.log("Snapshot for VM: "+vm.name+" successfully created.");
}

System.log("All snapshots created.")

Part 2 – vRO Workflow that creates snapshots for all virtual machines inside a folder and sends an email to a specified address with a list of completed and failed snapshots.

In this part, we expand the previous workflow. The workflow will continue to run if some snapshots fail. In the end, an email will be sent with a summary of which snapshots are completed and which are not.

vRO-FolderSnap2

We need some inputs from the user/administrator:
vmFolder (VC:VmFolder) – It is our folder where VMs are.
name (string) – Name of snapshot.
description ( string) – Snapshot description.
memory (boolean) – If yes, include virtual machine’s memory.
quiesce (boolean) – If yes, quiesce guest file system(requires VM tools).
toAddress (string) – Email address where we want to send a summary of snapshot creation tasks.

vRO-FolderSnap2a

Also we need to define some variables:
allVms (Array/VC:VirtualMachine) – List of all VMs inside folder. We will get it through vRO action.
task (VC:Task) – We will use it to check if task in vCenter Server is finished.
progress (boolean) – Monitor task progress.
pollRate (number) – Polling rate for the task state [seconds].
content (string) – Email content.
errorCode (string) – Variable to catch errors.
fromAddress (string) – Email from address.
fromName (string) – Email from friendly name.
snapCreated (Array/VC:VirtualMachine) – List of VMs that snapshot tasks finished successfully.
snapFailed (Array/VC:VirtualMachine) – List of VMs that snapshot tasks failed.
subject (string) – Email subject.

vRO-FolderSnap2b

First element is an vRO action called: com.vmware.library.vc.folder/getAllVirtualMachinesByFolderIncludingSubFolders
Inputs:
vmFolder (VC:VmFolder)
Outputs:
allVms (Array/VC:VirtualMachine)

The second element is a scriptable task. Here we create snapshots and make lists of successfully created snapshots and failed snapshots.
Inputs:
allVms (Array/VC:VirtualMachine)
description ( string)
name (string)
memory (boolean)
quiesce (boolean)
task (VC:Task)
progress (boolean)
pollRate (number)
errorCode (string)
snapCreated (Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)
Ouputs:
snapCreated (Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)

for each (var vm in allVms)
{
    try
    {
        //Create snapshot for VM.
        task = System.getModule("com.vmware.library.vc.vm.snapshot").createSnapshot(vm,name,description,memory,quiesce);
        //Wait until task is completed.
        System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
        System.log("Snapshot for VM: "+vm.name+" successfully created.");

        //Add VM to list
        snapCreated.push(vm);
    }
    catch (errorCode)
    {
        System.log("Snapshot for VM: "+vm.name+" failed.");
        snapFailed.push(vm);
    }
}

//Log successfully created snapshots.
System.log("Snapshots created for VMs:")
for each (var cVM in snapCreated)
{
    System.log(cVM.name);
}

//Log failed snapshots.
System.log("Snapshots failed for VMs:")
for each (var fVM in snapFailed)
{
    System.log(fVM.name);
}

The third element is a scriptable task where we can prepare our email content. It is up to you how it will look like.
Inputs:
snapCreated (Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)
Ouputs:
content (string)
subject(string)

subject = "Snapshot creation summary.";
content = "Successfully created snapshots for VM: <br />";
for each (var cVM in snapCreated)
{
    content += cVM.name+"<br />";
}

content += "<p style=\"color:red\"> Failed to create snapshots for VM:</p>";
for each (var fVM in snapFailed)
{
    content += fVM.name+"<br />";
}

The last element is a little modified workflow found in: Library/Mail/Send notification (TLSv1.2).
I just cut some unnecessary fields like SMTP Host or Port. I have only one SMTP endpoint added to that will be chosen as default.
Inputs:
content (string)
subject(string)
fromAddress (string)
fromName (string)
toAddress (string)
Below example of email:

vRO-FolderSnap2c

Part 3 – vRO Workflow that removes all snapshots for all virtual machines inside a folder.

Once again, workflow as simple as it can be. All out of the box.

vRO-FolderRemSnap1

We need some inputs from the user/administrator:
vmFolder (VC:VmFolder) – It is our folder where VMs are.

vRO-FolderRemSnap1a

And some variables:
allVms (Array/VC:VirtualMachine) – List of all VMs inside folder. We will get it through vRO action.
task (VC:Task) – We will use it to check if task in vCenter Server is finished.
progress (boolean) – Monitor task progress.
pollRate (number) – Polling rate for the task state [seconds].

vRO-FolderRemSnap1b

The first element is an vRO action called: com.vmware.library.vc.folder/getAllVirtualMachinesByFolderIncludingSubFolders
Inputs:
vmFolder (VC:VmFolder)
Outputs:
allVms (Array/VC:VirtualMachine)

The second element is a scriptable task.
Inputs:
allVms (Array/VC:VirtualMachine)
task (VC:Task)
progress (boolean)
pollRate (number)

for each (var vm in allVms)
{
    //Remove snapshots for VM.
    task = System.getModule("com.vmware.library.vc.vm.snapshot").removeAllSnapshot(vm)
    //Wait until task is completed.
    System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
    System.log("Snapshots for VM: "+vm.name+" successfully removed.");
}

System.log("All snapshots removed.")

Part 4 – vRO Workflow that removes all snapshots for all virtual machines inside a folder and sends an email to a specified address with a list of completed and failed tasks.

In this part, we expand the previous workflow that removes all snapshot. The workflow will continue to run if some tasks fail. In the end, an email will be sent with a summary of which taks are completed and which are not.

vRO-FolderRemSnap2

We need some inputs from the user/administrator:
vmFolder (VC:VmFolder) – It is our folder where VMs are.
toAddress (string) – Email address where we want to send a summary of snapshot removal tasks.

vRO-FolderRemSnap2a

And we need to define some variables:
allVms (Array/VC:VirtualMachine) – List of all VMs inside folder. We will get it through vRO action.
task (VC:Task) – We will use it to check if task in vCenter Server is finished.
progress (boolean) – Monitor task progress.
pollRate (number) – Polling rate for the task state [seconds].
content (string) – Email content.
errorCode (string) – Variable to catch errors.
fromAddress (string) – Email from address.
fromName (string) – Email from friendly name.
snapRemoved (Array/VC:VirtualMachine) – List of VMs that snapshot tasks finished successfully.
snapFailed (Array/VC:VirtualMachine) – List of VMs that snapshot tasks failed.
subject (string) – Email subject.

vRO-FolderRemSnap2b

First element is an vRO action called: com.vmware.library.vc.folder/getAllVirtualMachinesByFolderIncludingSubFolders
Inputs:
vmFolder (VC:VmFolder)
Outputs:
allVms (Array/VC:VirtualMachine)

The second element is a scriptable task. Here we remove snapshots and make lists of successfully removed snapshots and failed taks.
Inputs:
allVms (Array/VC:VirtualMachine)
task (VC:Task)
progress (boolean)
pollRate (number)
errorCode (string)
snapRemoved (Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)
Ouputs:
snapRemoved(Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)

for each (var vm in allVms)
{
    try
    {
        //Remove all snapshots for VM.
        task = System.getModule("com.vmware.library.vc.vm.snapshot").removeAllSnapshot(vm);
        //Wait until task is completed.
        System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,progress,pollRate);
        System.log("Snapshots for VM: "+vm.name+" successfully removed.");

        //Add VM to list
        snapRemoved.push(vm);
    }
    catch (errorCode)
    {
        System.log("Deleting snapshots for VM: "+vm.name+" failed.");
        snapFailed.push(vm);
    }
}

//Log successfully removed snapshots.
System.log("Snapshots removed for VMs:")
for each (var cVM in snapRemoved)
{
    System.log(cVM.name);
}

//Log failed snapshots.
System.log("Deleting snapshots failed for VMs:")
for each (var fVM in snapFailed)
{
    System.log(fVM.name);
}

The third element is a scriptable task where we can prepare our email content. It is up to you how it will look like.
Inputs:
snapRemoved (Array/VC:VirtualMachine)
snapFailed (Array/VC:VirtualMachine)
Ouputs:
content (string)
subject(string)

subject = "Snapshot removal summary.";
content = "Successfully deleted snapshots for VM: <br />";
for each (var cVM in snapRemoved)
{
    content += cVM.name+"<br />";
}

content += "<p style=\"color:red\"> Failed to remove snapshots for VM:</p>";
for each (var fVM in snapFailed)
{
    content += fVM.name+"<br />";
}

The last element is a little modified workflow found in: Library/Mail/Send notification (TLSv1.2).
I just cut some unnecessary fields like SMTP Host or Port. I have only one SMTP endpoint added to that will be chosen as default.
Inputs:
content (string)
subject(string)
fromAddress (string)
fromName (string)
toAddress (string)
Below example of email:

vRO-FolderRemSnap2c

Part 5 – Instructions on how to add workflows as context action in vCenter Server.

In the end, we need to add our workflow as a context action to vCenter. Remember that first, you must integrate vCenter with vRO.
Go to vCenter->Orchestrator Home and click on Context Actions and then Add button.
Select all workflows that you want to add as a context action and choose type of objects: VM and Template Folder.

vRO-FolderSnapContext1
vRO-FolderSnapContext2

Finally, we can do a test.
Right click on Vm folder in vCenter Server and run Orchestrator workflow.

vRO-FolderSnapContext3
vRO-FolderSnapContext4

Summary

That was an example of how simple and fast you could automate your common tasks. I encourage you to look for and test your own ideas. Have fun!.

Advertisement

2 thoughts on “Create/Remove snapshots for all virtual machines inside VM folder using vRO Plugin

  1. Thank you for providing the information. I am working on automating the creation\deletion of snapshots in vRA\vRO. Can you explain how I set the workflow to run a schedule?

    Like

    1. If I understand your question, the easiest way is to create the workflow, and inside that drag and drop icon “Schedule workflow”.
      You will have a workflow to add other workflows to the scheduler. Or the second option is to use REST API in vRO.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s