Skip to content

Instantly share code, notes, and snippets.

@mfojtik
Created November 4, 2011 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfojtik/1339341 to your computer and use it in GitHub Desktop.
Save mfojtik/1339341 to your computer and use it in GitHub Desktop.
Add CDROM to Vsphere VM
require 'rubygems'
require 'rbvmomi'
machine_conf_spec = RbVmomi::VIM.VirtualMachineConfigSpec(
:deviceChange => [{
:operation => :add,
:device => RbVmomi::VIM::VirtualCdrom(
:key => 2,
:connectable => RbVmomi::VIM::VirtualDeviceConnectInfo(
:startConnected => true,
:connected => true,
:allowGuestControl => true
)
)}]
)
def find_vm(host, username, password, name)
vsphere = RbVmomi::VIM.connect(:host => host, :user => username, :password => password, :insecure => true)
rootFolder = vsphere.serviceInstance.content.rootFolder
vm = {}
rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).each do |dc|
dc.datastoreFolder.childEntity.collect do |datastore|
vm[:instance] = datastore.vm.find { |x| x.name == name }
if vm[:instance]
vm[:datastore] = datastore.name
break
end
end
break if vm[:datastore]
end
vm
end
instance = find_vm('xxxxxx.xxx.xxx.redhat.com', 'admin', 'password', VIRTUAL_MACHINE_ID)
instance.ReconfigVM_Task(:spec => machine_conf_spec).wait_for_completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment