New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make hostmanager work with DHCP addresses #86
Comments
I'm currently using the following custom resolver to assign the correct IP address of my eth1 device (I'm using this to assign a private IP in Vagrant):
|
Made several changes due to ip6 address etc. config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`vagrant ssh -c "/sbin/ifconfig eth1" | grep "inet addr" | tail -n 1 | egrep -o "[0-9\.]+" | head -n 1 2>&1`.split("\n").first[/(\d+\.\d+\.\d+\.\d+)/, 1]
end
end |
Hi, After the standard vagrant init, up and ssh
i install apache2 with the common command
after that i leave the box and shutdown it with vagrant halt, until this point i don't touch the Vagrantfile, it was the standard file created by vagrant. Now i want to update /etc/hosts on host but ONLY the /etc/hosts on hosts without modify the hostname on guest. I use private network dhcp like @stucki but the plugin don't update my /etc/hosts. My Vagrantfile looks like:
Is this a bug or do i something wrong? And how can i prevent that vagrant change the hostname on guest, i want only set the right private ip with a specific hostname into /etc/hosts on host. |
If you know you're using VirtualBox as a provider, this is significantly faster than SSHing in (for me anyway):
Updated with @m1keil's suggestion, thanks. Also wrapped in a conditional to handle upping groups of VMs, some of which might not be created yet. |
halfninja thx for that config line. |
Another possible edit is to use Ruby's split() instead of AWK, so this will work on Windows as well: `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.split()[1] |
Yeah, that makes loads more sense - got caught up in the command line and forgot I was in Ruby :) |
+1 Custom resolver works, but having DHCP option supported natively would be great! |
+1 for support of DHCP-assigned private secondary interfaces. Also, any general solution should not rely on "vagrant ssh" to gain access into the VM. Be aware that the guests running Windows generally lack ssh access and instead use a winrm communicator. I share this not because I'm a Windows fanboy but because I have to deal with the darn guests and nifty plugins that rely on "vagrant ssh" cannot be used for networks of heterogeneous-os clients. The VBoxManage trick here avoids the vagrant ssh issue but has a similar issue with expecting all the clients to be provisioned to local VirtualBox. What's needed is a general solution that works regardless of the chosen communicators and the chosen providers. |
In case it's helpful to anyone, this variant doesn't use
|
+1 @ewencp awesome thanks. If anyone has trouble with, try changing |
Is |
Provider independent and works even with changed locales (german) which response with: config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`vagrant ssh -c "hostname -I"`.split()[1]
end
end |
+1 👍 @ewencp also works in multi machine set up 💯 |
@ewencp thanks. I modified your answer to work with CentOS 7 since ifconfig is not included by default. Now, if I can just get this to return the external IPs on the AWS instances only on the host I'll be set. config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if cached_addresses[vm.name].nil?
if hostname = (vm.ssh_info && vm.ssh_info[:host])
vm.communicate.execute("/usr/sbin/ip addr show eth0 | grep 'inet ' | xargs | cut -f 2 -d ' '| cut -f 1 -d '/' 2>&1") do |type, contents|
cached_addresses[vm.name] = contents.split("\n").first[/(\d+\.\d+\.\d+\.\d+)/, 1]
end
end
end
cached_addresses[vm.name]
end |
The centos7 impl for me was a ifesaver, thanks alot for this... so .. Is this hack still necessary? I think so. |
My version, combining @dominikzogg's use of config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if cached_addresses[vm.name].nil?
if hostname = (vm.ssh_info && vm.ssh_info[:host])
vm.communicate.execute("hostname -I | cut -d ' ' -f 2") do |type, contents|
cached_addresses[vm.name] = contents.split("\n").first[/(\d+\.\d+\.\d+\.\d+)/, 1]
end
end
end
cached_addresses[vm.name]
end |
Combining all the above as well as handling AWS private/public IP, Linux only but works on Redhat and Debian based system, and does not use $cached_addresses = {}
$ip_resolver = proc do |vm, resolving_vm|
# For aws, we should use private IP on the guests, public IP on the host
if vm.provider_name == :aws
if resolving_vm.nil?
used_name = vm.name.to_s + '--host'
else
used_name = vm.name.to_s + '--guest'
end
else
used_name= vm.name.to_s
end
if $cached_addresses[used_name].nil?
if hostname = (vm.ssh_info && vm.ssh_info[:host])
# getting aws guest ip *for the host*, we want the public IP in that case.
if vm.provider_name == :aws and resolving_vm.nil?
vm.communicate.execute('curl http://169.254.169.254/latest/meta-data/public-ipv4') do |type, pubip|
$cached_addresses[used_name] = pubip
end
else
vm.communicate.execute('uname -o') do |type, uname|
unless uname.downcase.include?('linux')
warn("Guest for #{vm.name} (#{vm.provider_name}) is not Linux, hostmanager might not find an IP.")
end
end
vm.communicate.execute('hostname --all-ip-addresses') do |type, hostname_i|
# much easier (but less fun) to work in ruby than sed'ing or perl'ing from shell
allips = hostname_i.strip().split(' ')
if vm.provider_name == :virtualbox
# 10.0.2.15 is the default virtualbox IP in NAT mode.
allips = allips.select { |x| x != '10.0.2.15'}
end
if allips.size() == 0
warn("Trying to find out ip for #{vm.name} (#{vm.provider_name}), found none useable: #{allips}.")
else
if allips.size() > 1
warn("Trying to find out ip for #{vm.name} (#{vm.provider_name}), found too many: #{allips} and I cannot choose cleverly. Will select the first one.")
end
$cached_addresses[used_name] = allips[0]
end
end
end
end
end
$cached_addresses[used_name]
end |
I found that I needed to wrap the communicate.execute in a "communicate.ready?" because otherwise issuing a vagrant destroy would fail because the machine is already down before cleaning up the /etc/hosts file:
Custom ip_resolver based on this issue with "if machine.communicate.ready?":
|
`Vagrant.configure("2") do |config|
config.vm.network :private_network, ip: "10.0.240.80"
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|if hostname = (vm.ssh_info && vm.ssh_info[:host])
|
Hello! Since this issue seems to SEO pretty well, as of vagrant 1.8.5 and later on ubuntu 16.10 (host) and Centos7 (guest), this line;
throws a nasty ruby exception saying,
I found a fix here that fixes the issue and does not rely on
Hopefully this helps people work around the regression in 1.8.5 and later. For full context here this is a gist of my complete Vagrantfile |
@Max-AR I have this error with your complete Vagrantfile in my context:
full log and Vagrantfile: https://gist.github.com/harobed/f3198e47417b026969fcae99b6cef39d |
My mistake: I forget this line
|
Hi,
When starting VM, I see a message about updating /etc/hosts
And in fact the two VM have a correct /etc/hosts and can communicate with each other. |
Without hack, in
with the hack, I don't have Then I think that the bug is always here. Context:
|
It's weird because I don't have the hack.
|
What say I think that your first interface isn't |
I got lo and eth0 in this order, for both machines. |
We upgraded to Xenial recently where the network interface layout is a bit different: ``` vagrant@beb7b8-projects-magweb-vgr:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:c3:0a:85 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3 valid_lft forever preferred_lft forever 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:5b:ca:2b brd ff:ff:ff:ff:ff:ff inet 172.28.128.3/24 brd 172.28.128.255 scope global enp0s8 valid_lft forever preferred_lft forever ``` Previously on precise it looked like this: ``` vagrant@beb7b8-projects-magweb-vgr:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:88:0c:a6 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:cd:10:81 brd ff:ff:ff:ff:ff:ff inet 172.28.128.4/24 brd 172.28.128.255 scope global eth1 valid_lft forever preferred_lft forever ``` It goes wrong [here](https://github.com/ByteInternet/hypernode-vagrant/blob/master/Vagrantfile#L105). This line: ``` ifconfig `find /sys/class/net -name 'eth*' -printf '%f\n' | tail -n 1` ``` Now returns nothing for the find so ifconfig will list all interfaces and the regex will match the wrong one (which isn't the shared interface). Instead we should also find the enp* interfaces like so: ``` ifconfig `find /sys/class/net \( -name 'eth*' -o -name 'enp*' \) -printf '%f\n' | sort | tail -n 1 ``` Also see devopsgroup-io/vagrant-hostmanager#86 (comment)
We upgraded to Xenial recently where the network interface layout is a bit different: ``` vagrant@beb7b8-projects-magweb-vgr:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:c3:0a:85 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3 valid_lft forever preferred_lft forever 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:5b:ca:2b brd ff:ff:ff:ff:ff:ff inet 172.28.128.3/24 brd 172.28.128.255 scope global enp0s8 valid_lft forever preferred_lft forever ``` Previously on precise it looked like this: ``` vagrant@beb7b8-projects-magweb-vgr:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:88:0c:a6 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:cd:10:81 brd ff:ff:ff:ff:ff:ff inet 172.28.128.4/24 brd 172.28.128.255 scope global eth1 valid_lft forever preferred_lft forever ``` It goes wrong [here](https://github.com/ByteInternet/hypernode-vagrant/blob/master/Vagrantfile#L105). This line: ``` ifconfig `find /sys/class/net -name 'eth*' -printf '%f\n' | tail -n 1` ``` Now returns nothing for the find so ifconfig will list all interfaces and the regex will match the wrong one (which isn't the shared interface). Instead we should also find the enp* interfaces like so: ``` ifconfig `find /sys/class/net \( -name 'eth*' -o -name 'enp*' \) -printf '%f\n' | sort | tail -n 1 ``` Also see devopsgroup-io/vagrant-hostmanager#86 (comment)
I have same, I have tried many ip_resolver, but no onne works
Update:
|
This is a complete fix example:
Check
|
If you use only virtualbox or parallels (i'm not sure about vmware) you can use read_guest_ip method from driver.
|
@RussianPenguin Do you ever get errors from Vagrant about guest property not being available only for subsequent VBoxManage command to show said property?
|
@andyfeller, no. I don't get any messages like this. Can you provide Vagrantfile and vagrant --version? |
Versions of everything:
Vagrant file
Vagrant up results
|
Try to remove or disable vagrant-vbguest. Sometimes installation of vbguest-additions cause problems with network: incorrect network interface startup after installing kernel modules (i can reproduce similar problem on macos).
|
have an eye on #250 |
For this, I had to put |
Tried the suggested
with vagrant 2.2.19, vagrant-hostmanager 1.8.9 and virtualbox 6.1.26, first VM start up fine, but the 2nd one is running into:
|
Thanks, @RussianPenguin - this still works in 2024. I'm wondering if this will ever get fixed, though... the plugin is basically useless if it doesn't pick the right IP. |
It seems like vagrant-hostmanager is reading the IP address of a host from the Vagrantfile. However, when using DHCP, that does not work.
The text was updated successfully, but these errors were encountered: