Using Ruby to Call vRO Workflows via the REST API

There are a bunch of posts out there showing how to connect to the REST API but I found there were very few that actually showed something interesting happening. So, I figured I’d throw together an quick ruby script showing how to create a snapshot. I used the rest-client gem but other than that, its fairly straightforward. If you are wondering how to use the vRO API in general, there is a great intro over at vCOTeam.info. The workflow ID I am calling is a stock workflow so the ID should be the same on your appliance but the user/pass may need to change if you modified them. You’ll also want to update the server and vcenter variables. Finally, the VM ID is specific to my home lab. You will need to get the VM ID of the VM you want to snapshot.
When you run the script, you should see a snapshot created on the VM defined in the script. Remember, the VM is defined by its vCenter ID, not its name. Here is a screenshot of the snapshot on a test VM in my lab.
snapshot image

You can download the script here.

#!/usr/bin/ruby

# Ruby script that connects to a VMware vRO server and kicks off a snapshot
# on a pre-defined vSphere VM. Could easily be extended to take an argument
# to get the VM.
#
# Written by Chris Adams, chris@linuxchris.com
# Written on 14 May 2016
#

require 'rest-client'
require 'openssl'
require 'date'

username = "vcoadmin"
password = "vcoadmin"
id = "BD80808080808080808080808080808053C180800122528313869552e41805bb1"
vm = "vm-43"
vcenter = "vcenter6.linuxchris.labnet"
server = "vco.linuxchris.labnet"
port = "8281"
apiuri = "/vco/api"

@debug = false

def buildUrl(username, password, server, port, apiuri, id)
# URL used to connect to the v(C)RO server
@url = "https://#{username}:#{password}@#{server}:#{port}#{apiuri}/workflows/#{id}/executions/"
puts "#{@url}" if @debug
end

def buildXml(vm, vcenter)
# Get current date/time so we can use it in the snapshot name
date = DateTime.now()
d = date.strftime("%Y%m%d-%H%M%S")
# Create the XML used to define the snapshot
@message = %Q( snap_from_api_#{d} false false )
puts "Message is: #{@message}" if @debug
end

buildUrl(username, password, server, port, apiuri, id)

buildXml(vm, vcenter)

client = RestClient::Resource.new(@url,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE,
:accept => 'application/xml',
:content_type => 'application/xml')

result = client.post "#{@message}", :content_type => 'application/xml'

This entry was posted in Automation, Ruby, Scripting, Sysadmin, vRO. Bookmark the permalink.

Leave a 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.