VMware Datastore selection based on Datastore Cluster

fine
automate

#1

I’m trying to modify vmware_best_fit_least_utilized method to select an eligible storage datastore that is located in a production or non-prod datastore cluster based on name. So far this is what I have, unfortunately other datastores match that are attached to the hosts that are not in the datastore clusters.

prov.eligible_hosts.each do |h|
  next if h.maintenance
  next unless h.power_state == "on"
  next unless h.name.start_with?(country_code)
  if country_code == 'us' || 'US'
    env = if tags[:environment] == 'production'
            'pr' || 'PR'
          else
            'np' || 'NP'
          end
    next unless h.name.include?(env)
  end
  nvms = h.vms.length
  if min_registered_vms.nil? || nvms < min_registered_vms
    storages = h.writable_storages.find_all { |s| s.free_space > vm.provisioned_storage } # Filter out storages that do not have enough free space for the Vm
    if country_code == 'us' || 'US'
      storages.keep_if { |v| v.name.include?(country_code) }.keep_if { |v| v.name.include?(env) }
    end
    s = storages.max_by(&:free_space)
    unless s.nil?
      host    = h
      storage = s
      min_registered_vms = nvms
    end
  end
end

#2

@gmccullough Can you help out here?


#3

so I did get this working now. Below is the block I changed to select a host who’s name starts with variable ‘country_code’ so it grabs the correct host in the correct vcenter attached to our manageiq as we have a couple from a few datacenters in different parts of the globe. After that it looks through storage and removes any that are not part of a datastore cluster so it’s ‘storage_clusters’ array is empty. Then ones that are part of a storage cluster we use 2 of them in each vcenter either prod or non-prod and thus this code is filtering for datastores that are associated with a DSC with a specific string in the DSC name.

prov.eligible_hosts.each do |h|
  next if h.maintenance
  next unless h.power_state == 'on'
  next unless h.name.downcase.start_with?(country_code)
  if country_code == 'us'
    env = if tags[:environment] == 'production'
            'pr'
          else
            'np'
          end
    next unless h.name.downcase.include?(env)
  end
  nvms = h.vms.length
  if min_registered_vms.nil? || nvms < min_registered_vms
    storages = h.writable_storages.find_all { |s| s.free_space > vm.provisioned_storage } # Filter out storages that do not have enough free space for the Vm
    if country_code == 'us'
      storages.delete_if { |v| v.storage_clusters.empty? }
      storages.keep_if { |v| v.storage_clusters.first[:name].downcase.include?(env) }
    end
    s = storages.max_by(&:free_space)
    unless s.nil?
      host    = h
      storage = s
      min_registered_vms = nvms
    end
  end
end