can we choose which inteface defines the $ipaddress fact?

3,240 views
Skip to first unread message

Gabriel Filion

unread,
Feb 7, 2011, 9:14:46 PM2/7/11
to puppet...@googlegroups.com
Hi,

I've run into some bug today with nagios checks that are exported in a
client's puppet setup.

The problem is that some host definitions are exported with an internal
address (10.x or 192.168.x) even though the servers do have an external
IP on another interface.

One of those uses a tap inteface to bridge traffic, and another has a
virtual interface that is used for a vlan.

In all cases, the interface with an internal IP comes up higher in the
output of ifconfig, and this makes the fact "ipaddress" take it as its
value.

Is there a way to force facter to chose a specific interface for the
ipaddress value?

--
Gabriel Filion

Patrick

unread,
Feb 8, 2011, 12:05:02 AM2/8/11
to puppet...@googlegroups.com

Yes and no.

Try running "facter | grep ipaddress" on the client. This will give you the names of variables you should be using instead that are listed by interface.

Gabriel Filion

unread,
Feb 8, 2011, 2:03:01 PM2/8/11
to puppet...@googlegroups.com, Patrick
On 11-02-08 12:05 AM, Patrick wrote:
> Yes and no.
>
> Try running "facter | grep ipaddress" on the client. This will give you the names of variables you should be using instead that are listed by interface.

hmm, well thanks for confirming this. I'll have to put some effort on my
nagios module to provide for an override for the ip address, then.

--
Gabriel Filion

Brian

unread,
Feb 8, 2011, 3:19:25 PM2/8/11
to Puppet Users
On Feb 7, 9:14 pm, Gabriel Filion <lelu...@gmail.com> wrote:
> In all cases, the interface with an internal IP comes up higher in the
> output of ifconfig, and this makes the fact "ipaddress" take it as its
> value.
>
> Is there a way to force facter to chose a specific interface for the
> ipaddress value?

I feel your pain! I have two facts, ipaddress_public and
ipaddress_private, that I defined to work around this.

http://polibyte.com/blog/getting_ip_addresses_with_facter

All the best,
Brian Pitts

Patrick

unread,
Feb 8, 2011, 3:30:40 PM2/8/11
to puppet...@googlegroups.com

This is really interesting.

One change you might want to consider making is to say that a 169.*.*.* ipaddresses doesn't count as having a valid address.

Naresh V

unread,
Feb 9, 2011, 1:01:47 AM2/9/11
to puppet...@googlegroups.com
On 8 February 2011 07:44, Gabriel Filion <lel...@gmail.com> wrote:
[...]

> Is there a way to force facter to chose a specific interface for the
> ipaddress value?

Hi,

I was wondering *when* would this part of the ipaddress.rb fact code
would be used:

--(0)> tail -20 /usr/lib/ruby/site_ruby/1.8/facter/ipaddress.rb

Facter.add(:ipaddress, :timeout => 2) do
setcode do
if hostname = Facter.value(:hostname)
# we need Hostname to exist for this to work
host = nil
if host = Facter::Util::Resolution.exec("host #{hostname}")
list = host.chomp.split(/\s/)
if defined? list[-1] and
list[-1] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
list[-1]
end
else
nil
end
else
nil
end
end
end
--(naresh.ve@puppet)-(~)--
--(0)> irb -rfacter
irb(main):001:0> Facter::Util::Resolution.exec("host puppet.domain.com")
=> "puppet.directi.com has address a.b.c.d"
irb(main):002:0> host = Facter::Util::Resolution.exec("host puppet.domain.com")
=> "puppet.directi.com has address a.b.c.d"
irb(main):003:0> list = host.chomp.split(/\s/)
=> ["puppet.directi.com", "has", "address", "a.b.c.d"]
irb(main):004:0> if defined? list[-1] and list [-1] =~
/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
irb(main):005:1> list[-1]
irb(main):006:1> end
=> "a.b.c.d"
irb(main):007:0>

(masked my IP with a.b.c.d)


If this part were the default/first piece of code that'd be used to
produce the ipaddress fact, setting the right A record for the fqdn
should solve the problem?

-Naresh

Gabriel Filion

unread,
Feb 9, 2011, 9:23:28 AM2/9/11
to puppet...@googlegroups.com, Naresh V
On 11-02-09 01:01 AM, Naresh V wrote:
> Facter.add(:ipaddress, :timeout => 2) do
> setcode do
> if hostname = Facter.value(:hostname)
> # we need Hostname to exist for this to work
> host = nil
> if host = Facter::Util::Resolution.exec("host #{hostname}")
> list = host.chomp.split(/\s/)
> if defined? list[-1] and
> list[-1] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
> list[-1]
> end
> else
> nil
> end
> else
> nil
> end
> end
> end

> If this part were the default/first piece of code that'd be used to


> produce the ipaddress fact, setting the right A record for the fqdn
> should solve the problem?

hmm this fact looks like it won't work for any host that doesn't
resolve, which would be problematic for something like a test setup with
virtual machines.

--
Gabriel Filion

Klaus Ethgen

unread,
Feb 9, 2011, 11:41:06 AM2/9/11
to puppet...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hmm.. I had similar problem without solving it the elegant way until
now.

But how about the following algorithm:
- - Check which interface has the default routing
- - If no default routing take the first one

Regards
Klaus
- --
Klaus Ethgen http://www.ethgen.ch/
pub 2048R/D1A4EDE5 2000-02-26 Klaus Ethgen <Kl...@Ethgen.de>
Fingerprint: D7 67 71 C4 99 A6 D4 FE EA 40 30 57 3C 88 26 2B
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQEVAwUBTVLDoZ+OKpjRpO3lAQo4Cgf/Z59ICBNqseZbCSi/AZMuEL3DlrT7xSiN
7nLLEV8beJTmBg9ueAoJhu5gDvvWzJf8rUq2yIlW1TW7sTFBAem+hWSnQbE4CBRc
X71HW1asuuK6OipzjEauJKz6o3zJ3hzpQ8EY+gEW9O+5N7Ud13XJ66kOTeukpdRd
USncCYHXszpjrM2GAEEegh+JXGr7Jd9h+VvKIuaa1vlKzfvwyNFZAfrImP6ZXbcc
RxkUAVpvi+QHWqwYZh2a5yqYcNkbcmjy43PW7km9QtHTWgHefpeuNzzcKO2aJrAr
5tBwk7lHVAclWydwplCdLIZK0Nq2BvUXOfL/SCSoFB/7DcMNtzg63A==
=tOoD
-----END PGP SIGNATURE-----

Daniel Pittman

unread,
Feb 13, 2011, 10:02:02 PM2/13/11
to puppet...@googlegroups.com
On Wed, Feb 9, 2011 at 08:41, Klaus Ethgen <Klaus+...@ethgen.de> wrote:

Sorry for jumping in late to this discussion, but:

To answer the original query, "is it possible to change which IP
address is used?":

No, it is not possible, because this is a daft fact that *claims* to
deliver something that is, in fact, not an actual bit of information.
So, don't expect significant work to "improve" the ipaddress fact,
which is a sow's ear, and unlikely to ever become a silk purse.

As to...

> Hmm.. I had similar problem without solving it the elegant way until
> now.
>
> But how about the following algorithm:
> - - Check which interface has the default routing
> - - If no default routing take the first one

...the proposed algorithm fails entirely on machines with more than
one "default" route, or without a default route at all, because it
will pick the wrong answer for a subset of systems. It /might/ be a
smaller subset of systems but, honestly, I doubt it would be. The
only place it really works is a single-address system with a single
NIC, and those usually don't have an extra address to fool the tool
anyhow.

The correct answer is that if you believe that your machine has a
primary IP address of some description then, indeed, you need to
supply a custom fact that determines whatever it is by the rules of
your own network.

Historically, I actually found it much more valuable to use this sort
of construction:

# The syntax is from memory, and I always get generate wrong, but
you should get the idea.
$main_ip = generate('/usr/local/bin/get-ip-from-dns-for', $fqdn)

That would run on the master and consult an authoritative database to
get the identity of a machine by name, which is generally what is
actually desired in this particular fact.

Also notable is the fact that using appropriate convention allows you
to have *multiple* identities for machines with multiple addresses,
something that is almost always a true situation for such hosts.

Regards,
Daniel
--
⎋ Puppet Labs Developer – http://puppetlabs.com
✉ Daniel Pittman <dan...@puppetlabs.com>
✆ Contact me via gtalk, email, or phone: +1 (877) 575-9775
♲ Made with 100 percent post-consumer electrons

Reply all
Reply to author
Forward
0 new messages