Vagrant and Ohai / Chef IP Address Hack



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:

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


How to have chef use a different-ip?


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:


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

4 thoughts on “Vagrant and Ohai / Chef IP Address Hack

  1. “`
    vagrant_ip = node[‘network’][‘interfaces’][‘eth1’][‘addresses’].detect{|k,v| v[‘family’] == “inet” }.first

  2. 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

    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!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.