Saturday, December 8, 2012

Puppet Custom Fact - Host Interface

I've found this simple custom fact extremely useful. It basically does a netstat -rn and grabs the default interface that has the gateway. I've tested it on FreeBSD, Redhat, and Ubuntu, it probably works on other platforms.

To install this module on your puppet master
mkdir -p /etc/puppet/modules/hostint/lib/facter
Copy the following code and place it in the file "/etc/puppet/modules/hostint/lib/facter/hostint.rb"
require 'facter'
Facter.add(:hostint) do
  confine :kernel => %w{FreeBSD}
  setcode do
    Facter::Util::Resolution.exec("netstat -f inet -rn | awk '$1==\"default\" { print $6 }'")
  end
end

Facter.add(:hostint) do
  confine :kernel => %w{Linux}
  setcode do
    Facter::Util::Resolution.exec("netstat -rn | awk '$1==\"0.0.0.0\" { print $8 }'")
  end
end
Ensure you have pluginsync enabled in your nodes puppet.conf
pluginsync = true
Now you are free to use this fact name in any module/manifest
<%= hostint %>
You can also download the module on the puppet forge http://forge.puppetlabs.com/panaman/hostint/0.0.1
Or install it straight from your puppet master
puppet module install panaman-hostint

No comments:

Post a Comment