Skip to content

Instantly share code, notes, and snippets.

@nohtyp
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save nohtyp/0a6fdea677b20fac5443 to your computer and use it in GitHub Desktop.

Select an option

Save nohtyp/0a6fdea677b20fac5443 to your computer and use it in GitHub Desktop.
This gist will add a cd that has an iso to vsphere 5.1
#!/usr/bin/env ruby
require 'yaml'
require 'rbvmomi'
hosts = ['VCenter host']
datacenters = ['datacenter']
templates = ['relativepath/vm']
mount = true
config_file = File.expand_path('~/.fog')
found_file = File.file?(config_file)
if "#{config_file}" == 'false'
puts 'The ~/.fog file does not exist'
else
puts "loading file"
mycreds = YAML.load_file(config_file)
#create loop
#log_in = #RbVmomi::VIM.connect host:
log_in = RbVmomi::VIM.connect host:"#{hosts[0]}", user: "#{mycreds['vsphere_username']}", password: "#{mycreds['vsphere_password']}", insecure: true
#create loop
dc = log_in.serviceInstance.find_datacenter("#{datacenters[0]}") or fail "datacenter not found"
vm = dc.find_vm("#{templates[0]}") or fail "VM not found"
d_obj = vm.config.hardware.device.select {|hw| hw.class == RbVmomi::VIM::VirtualCdrom}.first
machine_conf_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
deviceChange: [{
operation: :edit,
device: RbVmomi::VIM::VirtualCdrom(
backing: d_obj.backing,
key: d_obj.key,
controllerKey: d_obj.controllerKey,
connectable: RbVmomi::VIM::VirtualDeviceConnectInfo(
startConnected: "#{mount}",
connected: "#{mount}",
allowGuestControl: true)
)}])
vm.ReconfigVM_Task(spec: machine_conf_spec).wait_for_completion
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment