Skip to content

Instantly share code, notes, and snippets.

@rubiojr
Created September 5, 2011 15:28
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save rubiojr/1195252 to your computer and use it in GitHub Desktop.
Save rubiojr/1195252 to your computer and use it in GitHub Desktop.
Ruby VMWare VSphere API Example 1 (rbvmomi)
require 'rubygems'
require 'rbvmomi'
require 'pp'
require 'alchemist'
hyper = 'thunder03'
vim = RbVmomi::VIM.connect :host => hyper, :user => 'root', :password => 'secret', :insecure => true
#
# get current time
#
vim.serviceInstance.CurrentTime
#
# get datacenter
#
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.Datacenter.html
dc = vim.serviceInstance.find_datacenter
# get host object
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.HostSystem.html
host = dc.hostFolder.children.first.host.first
#
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.HardwareInfo.html
#
# host.hardware
#
puts host.hardware.memorySize.bytes.to.megabytes
puts host.hardware.cpuInfo.numCpuCores
#
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.RuntimeInfo.html
#
puts host.summary.runtime.powerState
#
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.QuickStats.html
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.html
puts host.summary.quickStats.overallMemoryUsage.megabytes
puts host.summary.quickStats.overallCpuUsage
puts host.summary.config.name
#
# Product Info
#
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.AboutInfo.html
#
puts host.summary.config.product.fullName
puts host.summary.config.product.apiType
puts host.summary.config.product.apiVersion
puts host.summary.config.product.osType
puts host.summary.config.product.productLineId
puts host.summary.config.product.vendor
puts host.summary.config.product.version
# List all VM
# http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html
vm = dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine).find do |x|
puts x.name
puts x.summary.config.memorySizeMB
puts x.summary.config.numCpu
puts x.summary.config.numEthernetCards
puts x.summary.config.numVirtualDisks
end
@toobulkeh
Copy link

You're a lifesaver. Thank you.

@doddo
Copy link

doddo commented May 22, 2014

This is vbery good, but to get all machines, you n eed recursion like described here: http://stackoverflow.com/questions/19812868/getting-all-virtualmachines-using-api-rbvmomi

@andrewfraley
Copy link

To get all VMs:

def list_vms(folder)
  children = folder.children.find_all
  children.each do |child|
    if child.class == RbVmomi::VIM::VirtualMachine
      puts child.name
    elsif child.class == RbVmomi::VIM::Folder
      list_vms(child)
    end
  end
end

@om-nishu-trantor
Copy link

Is there an example to edit .vmx file from vsphere api ?

@kerringtonwells
Copy link

Thanks!!!! Really Thanks

@rj-reilly
Copy link

When I run this, I get undefined method 'host' for line 22

@Maka7veli
Copy link

Can you please help on how to create a vm with rbvmomi! Please

@kartikaydw
Copy link

host = dc.hostFolder.children.first.host.first gives
NoMethodError: undefined method `first' for #RbVmomi::VIM::Folder:0x0000562d6b2f6388

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment