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


3 Replies to “Vagrant and Ohai / Chef IP Address Hack”

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

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 )

Google+ photo

You are commenting using your Google+ 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