VMware vRealize Automation – vRA7 – Infrastructure as Code (IaC)

vRA has a great UI for provisioning VM’s, but with developers needing infrastructure on demand and wanting to have the ability for continuous integration.  With the new and updated vRA API’s you can now easily code your own deployment scripts or recipes and integrate with your own run books and still get the the governance and lifecycle management that vRA has to offer.

Before I provide the provisioning script let’s break down each of the commands issued to get a better understanding of what we are doing and if needed you can tweak the script to your liking.  In this case I’m using a Linux machine to run a shell script that basically runs a series of curl commands to call REST API’s to vRA.

Download the vra-deploy.sh here

First I’ve defined some basic variables that can be used throughout the script:


Next lets setup the authentication and get the token from vRA for gaining access:

DATA=`curl -s –insecure -H “Accept: application/json” -H ‘Content-Type: application/json’ –data ‘{“username”:”‘”$USERNAME”‘”,”password”:”‘”$PASSWORD”‘”,”tenant”:”‘”$TENANT”‘”}’ https://$VRA/identity/api/tokens`
export TOKEN=`echo $DATA | grep -Po ‘(?<=”id”:”)[\w\d=]*(?=”)’`

AUTH=”Bearer $TOKEN”

Now let’s query the blueprint name that’s being requested and get the ID to use for outputing the JSON template for tweaking the blueprint properties or just to use for provisioning the blueprint:

#query blueprint
BLUEPRINT=`curl -s –insecure -H “Accept: $ACCEPT” -H “Authorization: $AUTH” https://$VRA/catalog-service/api/consumer/entitledCatalogItemViews?%24filter=name+eq+%27$NAME%27 | python -mjson.tool |  grep -Po ‘(?<=”catalogItemId”: “)[^”]*’`

This next command will output the template configuration of the blueprint.  This is great for modifying the blueprint in case you want to update the number of CPU’s, memory, lease etc.  If this is going to be common on editing the json template then it may be a good idea to put a pause into your script after this to give time to edit the template.  As you can see below this leverages python to help format the json output to better read and modify any settings as needed.  Also templates are saved in /tmp directory.

#create template
curl -s –insecure -H “Accept: $ACCEPT” -H “Authorization: $AUTH” https://$VRA/catalog-service/api/consumer/entitledCatalogItems/$BLUEPRINT/requests/template | python -m json.tool > /tmp/$NAME.json

Now that we have a blueprint template defined and possibly changed based on requirements it’s now time to provision:

#send request
REQUESTID=`curl -s –insecure -H “Accept: application/json” -H “Authorization: $AUTH” https://$VRA/catalog-service/api/consumer/entitledCatalogItems/$BLUEPRINT/requests –data @/tmp/$NAME.json –verbose -H “Content-Type: application/json” | python -m json.tool |  grep -Po ‘(?<=”id”: “)[^”]*’`

Machine is now being provisioned!  But we want to track progress until complete, so lets grab the request ID and query the request until complete:

#STATUS=`curl –insecure -H “Accept: $ACCEPT” -H “Authorization: $AUTH” https://$VRA/catalog-service/api/consumer/requests/${ID[2]} | python -mjson.tool | grep  -Po ‘(?<=”stateName”: “)[^”]*’`

#Get resource status
while true; do
        STATUS=$(curl -s –insecure -H “Accept: $ACCEPT” -H “Authorization: $AUTH” https://$VRA/catalog-service/api/consumer/requests/${ID[2]} | python -mjson.tool | grep  -Po ‘(?<=”stateName”: “)[^”]*’)
        if [ “$STATUS” == “Successful” ] || [ “$STATUS” == “Failed” ]
               END=$(date +%s.%N)
               DIFF=$(echo “$END – $START” | bc)
               echo -en  “\rDuration – $DIFF – Current status $STATUS”
               sleep 30

Last step now that the deployment is complete lets get some basic information about the machine name and IP address, so I can access the machine right away:

#Deployment Complete

if [ “$STATUS” == “Successful” ]
        echo -e “\nDeployment complete in $DIFF”
        #Get resource info
        curl -s –insecure -H “Content-Type: application/json” -H “Authorization: Bearer $TOKEN” https://$VRA/catalog-service/api/consumer/requests/${ID[2]}/resourceViews | python -m json.tool > /tmp/$NAME-${ID[2]}-results.json
        echo -e “\nHostname:” | cat /tmp/$NAME-${ID[2]}-results.json | grep -Po ‘(?<=”MachineName”: “)[^”]*’
        echo -e “\nIP Address:” | cat /tmp/$NAME-${ID[2]}-results.json | grep -Po ‘(?<=”ip_address”: “)[^”]*’
        echo -e “\nDeployment Failed in $DIFF”

Here’s a sample of the output when running:

I currently have the progress check every 30 seconds, but you can increase or decrease as needed.  I also add a duration to get an understanding how long it takes to deploy.

Lets look at the results after the request is complete.  As you can see the results show how long the deployment took in seconds (yes I know slow) as well as hostname and IP address.

Ready to give it a try in your environment?  Here’s the shell script I created to run on a Linux machine.  If you want to run from Windows I highly recommend to use Git Bash because it has the same capabilities and tools needed to accomplish the same tasks.

Linux Requirements:

Windows Requirements:
Git Bash – https://git-scm.com/download/win
Python (Note:  Make sure to define the python path in Git Bash by running – export PATH=”$PATH:/e/Python27″ assuming Python is installed on e:\Python27.

Download the vra-deploy.sh here

Usage:  ./vra-deploy.sh “<Blueprint Name>”

Want to do more with IaC like day two operations?  Here’s VMware’s official documentation on how to accomplish this from code:

vRealize Automation 7.0 Programming Guide

vRealize Automation 7.0 REST API Documentation


Leave a comment

%d bloggers like this: