vRO Workflow to unregister VMs in vRA

Hello!

Lately, I was thinking about what if we need to migrate our VMs somewhere or just from vRealize Automation. To do this with one VM you can just simply login to CloudClient and run forceunregister command. But I want to make bulk removal and they should be based on tenant names and prefixes of the VMs.

Let’s Begin!For achieving the goal we need:

  1. Linux VM – with installed CloudClient on it – we will use it as SSH host for running commands.
  2. vRO – to build our workflow
  3. vRA – with VMs which we gonna remove and Portal where we put a shiny button 🙂

Linux Part:

We have to make a script which will allow us to run CloudClient and run the commands on it:

#!/bin/bash
while getopts s:t:u:p:i:x:w: opt; do
case "$opt" in
s)
export vra_server=$OPTARG
;;
t)
export vra_tenant=$OPTARG
;;
u)
export vra_username=$OPTARG
;;
p)
export vra_password=$OPTARG
;;
i)
export vra_iaas_username=$OPTARG
;;
x)
export vra_iaas_password=$OPTARG
;;
w)
export vra_iaas_server=$OPTARG
;;
\?) usage;;
esac
done
shift $((OPTIND-1))
usage() {
echo "Invalid Arguments:"
echo "-s – vRA hostname"
echo "-t – Tenant Name"
echo "-u – vRA username"
echo "-p – vRA password"
echo "-i – IaaS username"
echo "-x – IaaS password"
echo "-w – IaaS server"
exit
}

if [ "$vra_server" == "" ]; then
usage
fi

if [ "$vra_tenant" == "" ]; then
vra_tenant="vsphere.local"
fi

export CLOUDCLIENT_SESSION_KEY="VRAPlugin-$vra_server$vra_tenant$vra_username"

cloudclient $@ | grep -v "CloudClient session:" | grep -v "vRA SSO login:" | grep -v "IaaS Model Manager login" > /root/script_results

chmod +rw /root/script_results

Also, I’ve added cloudclient and runvracommand.sh to /bin/ so they could run from anywhere. just make a soft link.

The hardest part is over.

vRO Part:

We have actually 2 nested Workflows used in this Workflow:

  1. RunCloudClientCommand – in here we make a construction of our SSH command for the easier use later – this commands run the Linux script with additional parameters like credentials and addresses of VRA and IaaS hosts.
    cmd = "runvracommand -s vra01.denis.lab -u vraadmin@vsphere.local -p Paswordooo! -t " + tenant + " -i administrator@denis.lab -x Passwordooo! -w vra-iaas01.denis.lab" + " \"" + command + "\""
  2. RunSSHCommand – Executes SSH command on the SSH host

 

  1. Our main workflow in first step runs the command “vra machines list” which is during the execution is written out to /root/script_result text file.
  2. Scriptable Task – Filter Output is creating the command to filter output in the script_results file:
    cmd = "cat /root/script_results | grep \"" + prefix +"\" | tr -d \" \" | cut -d "|\" -f 2"
  3. We run this SSH Command and map the output as VMsList – it is a string
  4. Scriptable Task – Construct Command – we actually make arrays of commands (one command for one VM):
    System.log("=========================\n Converting to array…")
    var VMsArray = VMListText.split("\n");
    commands = new Array();
    tenants = new Array();
    System.log(commands)for (i=0;i<VMsArray.length;i++) {
    System.log("\n ========================== \n removing VM – " + VMsArray[i] + "\n ================")
    removeCommand = "vra machines forceunregister –name " + VMsArray[i];
    System.log(removeCommand);
    commands.push(removeCommand);
    tenants.push(tenant);
    }
  5. And we use ForEach Workflow to run this commands (inputs are Arrays named commands and tenants which we created in previous step)

Our Workflow is ready.

vRA Part:

We need to create a XaaS blueprint and assign this workflow to it, choose a nice  icon for it. As it is a migration part i  thought that the briefcase should fit well.

 

That is it! Hope it will be useful for you.

2 responses to “vRO Workflow to unregister VMs in vRA”

  1. Alok avatar
    Alok

    Hi Rusov,

    Great article. My requirements are a little different. Instead of Deleting Vm’s based on Prefix can you help me modify this by providing a list of Vm’s to delete. Ive 700 of them to delete and they don’t have a prefix have random names.

    Thanks a lot for your support in advance

    Alok

    1. D.Rusov avatar
      D.Rusov

      Hi Alok,
      Its been a while since i did that one. But i guess for your case you could just use your list as a “script_result” file. And you wont need to filter anything as it is correct one from the start.
      BR,
      Denis.

Leave a Reply to D.Rusov Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.