diff --git a/lib/vagrant-hostmanager/hosts_file.rb b/lib/vagrant-hostmanager/hosts_file.rb index 1a175d1..505b82e 100644 --- a/lib/vagrant-hostmanager/hosts_file.rb +++ b/lib/vagrant-hostmanager/hosts_file.rb @@ -17,7 +17,7 @@ module VagrantPlugins next if ip end end - ip || machine.ssh_info[:host] + ip || (machine.ssh_info ? machine.ssh_info[:host] : nil) end # create the temporary hosts file @@ -29,17 +29,22 @@ module VagrantPlugins machines << machine = env.machine(name, provider) host = machine.config.vm.hostname || name ip = get_ip_address.call(machine) - host_aliases = machine.config.hostmanager.aliases.join("\s").chomp - machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', { - :ip => ip, - :host => host, - :aliases => host_aliases, - }) - file << "#{ip}\t#{host}\s#{host_aliases}\n" + if ip + host_aliases = machine.config.hostmanager.aliases.join("\s").chomp + machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', { + :ip => ip, + :host => host, + :aliases => host_aliases, + }) + file << "#{ip}\t#{host}\s#{host_aliases}\n" + else + machine.env.ui.warn I18n.t('vagrant_hostmanager.action.host_no_ip', { + :name => name, + }) + end end end end - machines end @@ -57,10 +62,26 @@ module VagrantPlugins end private + # Either use the active machines, or loop over all available machines and + # get those with the same provider (aka, ignore boxes that throw MachineNotFound errors). + # # Returns an array with the same structure as env.active_machines: # [ [:machine, :virtualbox], [:foo, :virtualbox] ] def get_machines(env, provider) - env.active_machines + if env.config_global.hostmanager.include_offline? + machines = [] + env.machine_names.each do |name| + begin + m = env.machine(name, provider) + machines << [name, provider] + rescue Vagrant::Errors::MachineNotFound => ex + # ignore this box. + end + end + machines + else + env.active_machines + end end end diff --git a/locales/en.yml b/locales/en.yml index 337d096..758b7d3 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -2,6 +2,7 @@ en: vagrant_hostmanager: action: add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}" + host_no_ip: "Could not determine ip for machine '%{name}': no private ip configured or machine not up." update: "[%{name}] Updating /etc/hosts file" config: not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"