- From: Geoff Meakin <
>
- To:
- Subject: [chef] Get most appropriate IP address from local node
- Date: Fri, 13 Jan 2012 19:22:20 +0000
Hi all, Sorry for the mammoth post!
Obviously I want the same chef code provisioning all my environments,
including production, dev, test, CI, etc..
One of the problems I came across is using Vagrant, as it provisions boxes
differently to AWS/Xen/Knife bootstrapping. I noticed that Vagrant adds an
extra interface card and IP address via host-only-networking to my dev boxes,
hence my usual call to node.automatic_attrs['ipaddress'] picks up the
VirtualBox provisioned one rather than the one I set via Vagrant… frustrating.
For example… I have a bunch of services which need to register themselves
with DNS. So I'm setting up an APT/YUM server say, and I want the creation of
the server to register apt.mydomain.com or yum.mydomain.com with the local
DNS server via dynamic dns. I had this all working on production using
node.automatic_attrs['ipaddress'].. but with vagrant+virtualbox, this picked
up the wrong interface ip address.
I solved this by writing a routine "most_relevant_primary_ip_address", which
resolves to one of any number of interfaces/IP addreses on the local box, and
checks them via the ruby-ip gem, as to whether they resolve to the correct
provisioning network, and thus using them. This is also useful in amazon and
other areas (e.g. IPVS clients) where I have multiple ip addresses going on.
At the moment, I've put the code in a library under networking_basic as
suggested in the online manual as follows:
networking_basic/libraries/network_utils.rb
require 'ip'
module NetworkUtils
class Chef::Recipe
def most_relevant_primary_ip_address
…. code
end
end
end
This seems to be the wrong place for it, the function doesn't really belong
to a Chef::Recipe.. It needs to be accessible everywhere from each cookbook,
and needs to run after ohai has done its stuff, and the node object has been
provisioned.
I would appreciate any pointers as to where else I could put this … and also
if I've just reinvented the wheel because there's already stuff out there to
do this.
Thanks!
Archive powered by MHonArc 2.6.16.