Vagrant and Ohai / Chef IP Address Hack

 

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

OHAI-287

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s