Implemented host_vars option.

This commit is contained in:
Christian Henz 2015-12-01 18:15:40 +01:00
parent a5dd61c450
commit 77b11a989c
4 changed files with 21 additions and 0 deletions

View File

@ -9,6 +9,7 @@ module VagrantPlugins
attr_accessor :galaxy_role_file
attr_accessor :galaxy_roles_path
attr_accessor :galaxy_command
attr_accessor :host_vars
attr_accessor :groups
attr_accessor :inventory_path
attr_accessor :limit
@ -27,6 +28,7 @@ module VagrantPlugins
@galaxy_role_file = UNSET_VALUE
@galaxy_roles_path = UNSET_VALUE
@galaxy_command = UNSET_VALUE
@host_vars = UNSET_VALUE
@groups = UNSET_VALUE
@inventory_path = UNSET_VALUE
@limit = UNSET_VALUE
@ -46,6 +48,7 @@ module VagrantPlugins
@galaxy_role_file = nil if @galaxy_role_file == UNSET_VALUE
@galaxy_roles_path = nil if @galaxy_roles_path == UNSET_VALUE
@galaxy_command = GALAXY_COMMAND_DEFAULT if @galaxy_command == UNSET_VALUE
@host_vars = {} if @host_vars == UNSET_VALUE
@groups = {} if @groups == UNSET_VALUE
@inventory_path = nil if @inventory_path == UNSET_VALUE
@limit = nil if @limit == UNSET_VALUE

View File

@ -70,6 +70,19 @@ module VagrantPlugins
end
end
def get_inventory_host_vars_string(machine_name)
vars = config.host_vars[machine_name]
s = nil
if vars.is_a?(Hash)
s = vars.each.collect{ |k, v| "#{k}=#{v}" }.join(" ")
elsif vars.is_a?(Array)
s = vars.join(" ")
elsif vars.is_a?(String)
s = vars
end
if s and !s.empty? then s else nil end
end
def generate_inventory
inventory = "# Generated by Vagrant\n\n"

View File

@ -128,6 +128,8 @@ module VagrantPlugins
else
machines += "#{machine_name}\n"
end
host_vars = get_inventory_host_vars_string(machine_name)
machines.sub!(/\n$/, " #{host_vars}\n") if host_vars
end
end

View File

@ -151,12 +151,15 @@ module VagrantPlugins
# Call only once the SSH and WinRM info computation
# Note that machines configured with WinRM communicator, also have a "partial" ssh_info.
m_ssh_info = m.ssh_info
host_vars = get_inventory_host_vars_string(m.name)
if m.config.vm.communicator == :winrm
m_winrm_net_info = CommunicatorWinRM::Helper.winrm_info(m) # can raise a WinRMNotReady exception...
machines += get_inventory_winrm_machine(m, m_winrm_net_info)
machines.sub!(/\n$/, " #{host_vars}\n") if host_vars
@inventory_machines[m.name] = m
elsif !m_ssh_info.nil?
machines += get_inventory_ssh_machine(m, m_ssh_info)
machines.sub!(/\n$/, " #{host_vars}\n") if host_vars
@inventory_machines[m.name] = m
else
@logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")