Changing VMWare adapter type based on Template OS

Here is how you can customize your network adapter type in VMWare based on what OS is getting provisioned. In this example, Windows templates requires VMXNET3 adapters while Linux VMs requires E1000 adapter.

# Get provisioning object
prov = $evm.root["miq_provision"]
vm_template = prov.vm_template

# Changing Adapter type based on Template OS
if vm_template.operating_system[:product_name].downcase.include?('windows')
   $evm.log("info", "WINDOWS DETECTED: SETTING UP NETWORK INTERFACE TO VMXNET3")
   prov.set_network_adapter(0, { :network => "VM Network", :devicetype => "VirtualVmxnet3", :is_dvs => false})
else
   $evm.log("info", "SETTING UP NETWORK INTERFACE TO E1000")
   prov.set_network_adapter(0, { :network => "VM Network", :devicetype => "VirtualE1000", :is_dvs => false})
end

Set VLAN based on environment tag

Replace your current “if set_vlan” code from your existing PreProvision method to set your VLAN based on the environment tag.

* This code assume that your service dialog as a dropdown named “tag_0_environment”.

tags = prov.get_tags
log(:info, "Provision Tags:<#{tags.inspect}>")

if set_vlan

  default_vlan = "Private"
  #default_dvs = "portgroup1"

  case tags[:environment].to_s
  when 'private'
    prov.set_vlan("Private")
    #prov.set_dvs(default_dvs)
    log(:info, "ENVIRONMENT tag = private; setting vlan to Private")
  when 'public'
    prov.set_vlan("Public")
    #prov.set_dvs(default_dvs)
    log(:info, "ENVIRONMENT tag = public; setting vlan to Public")
  else
    prov.set_vlan("Private")
    #prov.set_dvs(default_dvs)
    log(:info, "No ENVIRONMENT tag set, setting default: #{default_vlan}")
  end

end

my Method template

###################################
#
# CFME Automate Method: <method_name>
#
# by Marco Berube
#
# Notes: ******************
#
###################################
begin
 # Method for logging
 def log(level, message)
 @method = 'change_me_for_method_name'
 $evm.log(level, "#{@method}: #{message}")
 end
 # dump_root
 def dump_root()
 log(:info, "Root:<$evm.root> Begin $evm.root.attributes")
 $evm.root.attributes.sort.each { |k, v| log(:info, "Root:<$evm.root> Attribute - #{k}: #{v}")}
 log(:info, "Root:<$evm.root> End $evm.root.attributes")
 log(:info, "")
 end

 # dump all root attributes to the log
 dump_root

 #
 # ADD YOUR CODE HERE
 #

 # Exit method
 log(:info, "CFME Automate Method Ended")
 exit MIQ_OK

 # Error Rescue
rescue => err
 log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
 exit MIQ_STOP
end

Naming VMs based on Group name

One of my customer asked me to name his VMs based on the group name provisioning that VM.  Example:  redgroup-001

Turns out that you cannot use the usual naming method as services are always provisioned as “super-administrator” in Cloudforms 3.0.

I added the following code in my “CustomizeRequest” to get this done:

  1.   #
  2.   # Changing VM name based on Group:  groupname_###
  3.   #
  4.  
  5.   miq_request = prov.miq_request
  6.   user = miq_request.requester
  7.   group = user.miq_group
  8.  
  9.   log(:info, “User: #{user.name} Group: #{group.description}”)
  10.   vm_name = “#{group.description}_”
  11.    
  12.     # Loop through 0-999 and look to see if the name already exists in CFME
  13.     for i in (1..999)
  14.       new_vm_name = “#{vm_name}#{i}”
  15.       if $evm.vmdb(‘vm’).find_by_name(new_vm_name).nil?
  16.         log(:info, “Using VM:<#{new_vm_name}> name”)
  17.         break
  18.       end
  19.     end
  20.   prov.set_option(:vm_target_name, new_vm_name)
  21.   log(:info, “Adding Option prov.set_option(:vm_target_name, #{prov.get_option(:vm_target_name)}) to Provisioning ID:<#{prov.id}>”)
  22.   prov.set_option(:vm_target_hostname, new_vm_name)
  23.   log(:info, “Adding Option prov.set_option(:vm_target_hostname, #{prov.get_option(:vm_target_hostname)}) to Provisioning ID:<#{prov.id}>”)
  24.   prov.set_option(:vm_name, new_vm_name)
  25.   log(:info, “Adding Option prov.set_option(:vm_name, #{prov.get_option(:vm_name)}) to Provisioning ID:<#{prov.id}>”)