Problem
In vagrant, ohai returns the eth0 ip.
This is a PITA, since if you run clusters of Vagrant that use Chef (as I do), then Chef and ohai
thinks the IP address of the node is always:
10.0.1.15
or whatever Vagrant attaches to the default interface (usually eth0).
It’s Not Just Me
Plenty of people appear to have this problem:
How to change ip address of node after added to chef server?
Chef and ohai retrieving a droplets private ip address
http://chef.opscode.narkive.com/TFd0kvF0/force-main-ip-on-multi-homed-chef-nodes
How to have chef use a different-ip?
Solution
I haven’t found a ‘proper’ solution for this. The most elegant (or least inelegant) one I could find involves:
- Finding the ruby file involved in determining network information
- Getting the IP address associated with the interface that you want Chef to ‘see’
- (Optional) get the mac address associated with the interface that you want Chef to ‘see’
- Hard-code the IP address and macaddress with these values directly in the
network.rb
file
Here’s an example:
RUBYFILE='/opt/chef/embedded/lib/ruby/gems/2.4.0/gems/ohai-13.5.0/lib/ohai/plugins/linux/network.rb' IPADDR=$(ip addr show eth1 | grep -w inet | awk '{print $2}' | sed 's/\(.*\).24/\1/'""") MACADDRESS=$(ip addr show eth1 | grep -w link.ether | awk '{print $2}'""") sed -i "s/\(.*${IPADDR} \).*/\1 \"\"/;s/\(.*macaddress \)m.*/\1 \"${MACADDRESS}\"/" $RUBYFILE
Got a Better Way?
Please let me know!
Author is currently working on the second edition of Docker in Practice
Get 39% off with the code: 39miell2
“`
vagrant_ip = node[‘network’][‘interfaces’][‘eth1’][‘addresses’].detect{|k,v| v[‘family’] == “inet” }.first
“`
Fails if eth1 isn’t the right iface.
I have seen situations where the eth0 / eth1 are swapped (presumably some kind of race condition). I’ve had to add
Also, the name of the interface can be unpredictable if the host is unknown.
I ended up doing this to capture the info:
https://github.com/ianmiell/shutit-openshift-cluster/blob/master/shutit_openshift_cluster.py#L193-L195
There is a vagrant-ohai plugin which lets you do this.
Unfortunately it is broke. Simple 1 line fix is needed.
I fixed it and issued a pull request, waiting on the maintainer to merge it
https://github.com/avishai-ish-shalom/vagrant-ohai/pull/21
In the interim you can still use the plugin and make a simple 1 line change to it.
1. Install the plugin
vagrant plugin install vagrant-ohai
2. Edit the file vagrant-ohai-0.1.13/lib/vagrant-ohai/action_configure_chef.rb
I use a Mac and the plugin is installed in the ~/.vagrant.d/gems/2.4.4/gems/vagrant-ohai-0.1.13/lib/vagrant-ohai/action_configure_chef.rb directory
vi ~/.vagrant.d/gems/2.4.4/gems/vagrant-ohai-0.1.13/lib/vagrant-ohai/action_configure_chef.rb
Change line 22 from this:
tmp.puts ‘Ohai::Config[:plugin_path] << "/etc/chef/ohai_plugins"'
To this:
tmp.puts 'ohai.plugin_path << "/etc/chef/ohai_plugins"'
3. Edit your Vagrantfile and add this config statement to the vagrant virtual machine:
config.ohai.primary_nic = "eth1"
That's it!