Home > vSphere API > Ruby to Manage and Automate VMware vSphere?

Ruby to Manage and Automate VMware vSphere?

August 30th, 2010 Leave a comment Go to comments

As I mentioned in a previous blog, vSphere(VI) Java API can be used in any JVM languages/frameworks. We have samples in Jython, Groovy, Grail. This weekend I got a sample in JRuby shared by our community member Martin Jackson in the API forum. Thanks Martin!

I think it would be fun to share it with you. If you can write Ruby code, you can take advantage of VI Java API for managing and automating vSphere as well. If you have samples leveraging the API to share, I am happy to hear about it.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

Now, let us take a look at Martin’s sample code ported from a VI Java API sample.

#  VI Java API Hello using JRuby
require 'java'
require 'dom4j-1.6.1.jar'
require 'vijava2120100824.jar'

# Only using a single class from this package so I'll just import it
import java.net.URL
# Creating ViJava Module so I don't have to import each com.vmware.vim25.mo class individually.
module VIJava
  include_package "com.vmware.vim25.mo"
end

si = VIJava::ServiceInstance.new(URL.new("https://192.168.1.9/sdk"), "root", "password", true)
rootFolder = si.getRootFolder() vms = VIJava::InventoryNavigator.new(rootFolder).searchManagedEntities("VirtualMachine")
puts "Hello " + vms[0].getName()

si.getServerConnection().logout()