VMware Cloud Community
sc2317
Enthusiast
Enthusiast
Jump to solution

Delete Old Snapshots via vRO(vRealize Orchestrator)

Hi,

I want to schedule vRO workflow to delete old snapshot based on the its age(days). I know there is default workflow in vRO that we can use to delete old snapshots, however I need more information on that.

1) If we have more than 1 vCenter instances added in vRO, then does this vRP workflow runs on all vCenter servers or do we need to modify it as per our requirment.

2) Before running this on all VMs in Infra, I would first like to test on  2 ~ 3 specific machines in Infra. Can someone assist me in modifying default old snapshot workflow, so that it runs on these specific VMs only.

3) Is it also possible to exclude few VMs in infra that Workflow should not touch or delete their snapshots. If yes, then kindly assist how can this be done ?

Reply
0 Kudos
1 Solution

Accepted Solutions
sc2317
Enthusiast
Enthusiast
Jump to solution

My bad, I copied it from the wrong workflow. Below is the correct one for exclusion of specific VMs.

var logtext;

if(activeSnapshot.config){

var vmName = activeSnapshot.config.name;

var snapshotID = activeSnapshot.id;

if(vmName == "VMname1" || vmName == "VMname2")

{

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'does not need to be removed.";

}

else{

var task = activeSnapshot.removeSnapshot_Task(removeChildren,true);

var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task) ;

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'has been removed.";

}

}else{

logtext = "The snapshot with the id " + activeSnapshot.id + " has already been removed.";

}

content = content + "<br>" + logtext;

System.log(logtext);

View solution in original post

Reply
0 Kudos
7 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

1) Yes, it will collect snapshots from all vCenter servers registered in vRO. The workflow 'Delete old snapshots' calls scripting action 'com.vmware.library.vc.vm.snapshot.getAllSnapshotResultInDatastoreBrowser', and if you open the scripting code of this action and look around lines 17-18, you'll see that it iterates over all datastores on all vCenter servers.

2) and 3) Open the workflow 'Delete old snapshots' in vRO, and check the code inside the scriptable task labelled 'Get snapshots', which prepares the list of workflows to be processed. Toward the end of the code, there is a line

snapshots.push(ss);

that pushes the currently iterated snapshot ss to the list of snapshots to be processed snapshots. There, you should ask some scripting code that will check the snapshot before adding it to the list (see in vRO API Explorer what are the properties for the scripting objects of type VcVirtualMachineSnapshot), and do not add it to the list if you want to skip it (for example, if the snapshot property virtualMachineName matches the names of your infra VMs).

Hope this helps; let me know if further info is needed.

By the way, to modify the workflow 'Delete old snapshots', you'll need to duplicate it, because the workflows coming with the standard plug-ins are 'sealed' and the UI will prevent you to modify them.

Reply
0 Kudos
sc2317
Enthusiast
Enthusiast
Jump to solution

Hi,

Thanks, I have already duplicated the workflow. I am not sure what to update in the code as I am new to vRO and based on my knowledge of Powershell/PowerCLI, I can try to understand the Java-Script code inside the workflow. However, not sure how can I modify the code as per my requirement in vRO.

Could you or anyone suggest the code that needs to added or existing code update for below tasks.

1) Test delete old snapshot workflow for specific VMs . For example VMs with name name VM1 & VM2.

2) Once the above is tested successfully, Exclude few VMs so that workflow does not delete snapshot of these VMs. For example VMs with name VM3 & VM4

Thanks in advance.

Reply
0 Kudos
sc2317
Enthusiast
Enthusiast
Jump to solution

Managed to perform the test by editing the script of "Remove Snapshot" by adding below.Many thanks for help.

var logtext;

if(activeSnapshot.config){

var vmName = activeSnapshot.config.name;

var snapshotID = activeSnapshot.id;

if(vmName = "yourvmname")

{

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'does not need to be removed.";

}

else{

var task = activeSnapshot.removeSnapshot_Task(removeChildren);

var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task) ;

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'has been removed.";

}

}else{

logtext = "The snapshot with the id " + activeSnapshot.id + " has already been removed.";

}

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Well, the code shown in bold is not correct Smiley Happy (if executed as-is, no snapshot will be removed at all, for any VM).

Reply
0 Kudos
sc2317
Enthusiast
Enthusiast
Jump to solution

Yes, I wanted to exclude few vms from the deletion.

Similarly, if I want only specific vms snapshot to be removed then I can make it like below.

var logtext;

if(activeSnapshot.config){

var vmName = activeSnapshot.config.name;

var snapshotID = activeSnapshot.id;

if(vmName = "yourvmname")

{

var task = activeSnapshot.removeSnapshot_Task(removeChildren);

var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task) ;

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'has been removed.";

}

else{

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'does not need to be removed.";

}

}else{

logtext = "The snapshot with the id " + activeSnapshot.id + " has already been removed.";

}

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

This will also not work as intended, for the same reason as your previous code snippet - your condition check is incorrect. You are doing assignment, not equality check.

Reply
0 Kudos
sc2317
Enthusiast
Enthusiast
Jump to solution

My bad, I copied it from the wrong workflow. Below is the correct one for exclusion of specific VMs.

var logtext;

if(activeSnapshot.config){

var vmName = activeSnapshot.config.name;

var snapshotID = activeSnapshot.id;

if(vmName == "VMname1" || vmName == "VMname2")

{

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'does not need to be removed.";

}

else{

var task = activeSnapshot.removeSnapshot_Task(removeChildren,true);

var actionResult = System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task) ;

logtext = "The snapshot with the id " + snapshotID + " from the Virtual machine '"+vmName+"'has been removed.";

}

}else{

logtext = "The snapshot with the id " + activeSnapshot.id + " has already been removed.";

}

content = content + "<br>" + logtext;

System.log(logtext);

Reply
0 Kudos