Created
November 4, 2011 13:41
-
-
Save mfojtik/1339341 to your computer and use it in GitHub Desktop.
Add CDROM to Vsphere VM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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