Hello,
Our team wanted host names be constructed as follows:
[prefix] + [environment tag] + [cost center] + [zero padded number]
The cost center
bit is the new piece to be added.
So if our prefix is int
, environment tag is blank, cost center is a1
an example hostname would be: `inta1001.
This is not difficult to accomplish. I thought I’d share how we did it for anyone else hoping to make a similar customization. I should note first that the foundational knowledge required to make this change is found in this chapter on VM Naming During Provisioning (updated version of that chapter found here)from Peter McGowan (@pemcg). Read that first. It explains how to “activate” the naming logic (using “changeme” for vm name) and how to customize your prefix value. After making those changes, simply customize the vmname.rb method found at Infrastructure/VM/Provisioning/Naming/methods
We altered the contents of the else
clause in the derived_name
function, and then added the cost_center
function and the combine_env_tags_and_cost_center
function:
# / Infra / VM / Provisioning / Naming / default (vmname)
#
# Description: This is the default naming scheme
# 1. If VM Name was not chosen during dialog processing then use vm_prefix
# from dialog else use model and [:environment] tag to generate name
# 2. Else use VM name chosen in dialog
# 3. Add 5 digit suffix to vmname if more than one VM is being provisioned
#
module ManageIQ
module Automate
module Infrastructure
module VM
module Provisioning
module Naming
class VmName
def initialize(handle = $evm)
@handle = handle
end
def main
@handle.log("info", "Detected vmdb_object_type:<#{@handle.root['vmdb_object_type']}>")
@handle.object['vmname'] = derived_name.compact.join
@handle.log(:info, "vmname: \"#{@handle.object['vmname']}\"")
end
def derived_name
if supplied_name.present?
[supplied_name, suffix(true)]
else
[prefix, combine_env_tags_and_cost_center, suffix(false)]
end
end
def supplied_name
@supplied_name ||= begin
vm_name = provision_object.get_option(:vm_name).to_s.strip
vm_name unless vm_name == 'changeme'
end
end
def provision_object
@provision_object ||= begin
@handle.root['miq_provision_request'] ||
@handle.root['miq_provision'] ||
@handle.root['miq_provision_request_template']
end
end
# Returns the name prefix (preferences model over dialog) or nil
def prefix
@handle.object['vm_prefix'] || provision_object.get_option(:vm_prefix).to_s.strip
end
# Returns the first 3 characters of the "environment" tag (or nil)
def env_tag
env = provision_object.get_tags[:environment]
return env[0, 3] unless env.blank?
end
# Returns the cost center value from the service dialog:
def cost_center
cc = "#{@handle.root['miq_provision'].miq_provision_request.options[:dialog]["dialog_cost_center"]}".downcase
@handle.log("info", "The Cost Center to be used in forming the hostname is #{cc}.")
if cc == nil
cc = ""
end
return cc
end
# Returns the combined value of the cost center and the Environment tag (if any):
def combine_env_tags_and_cost_center
et = env_tag
@handle.log("info", "the environment tag to be used is #{et}.")
cc = cost_center
return "#{et}#{cc}"
end
# Returns the name suffix (provision number) or nil if provisioning only one
def suffix(condensed)
"$n{5}" if provision_object.get_option(:number_of_vms) > 1 || !condensed
end
end
end
end
end
end
end
end
ManageIQ::Automate::Infrastructure::VM::Provisioning::Naming::VmName.new.main
You’ll also notice that we changed the suffix function to use “$n{5}” instead of “$n{3}” to create a five-digit zero-padded number at the end of the name.
I hope this helps someone.