Published on 10 September 2010 by

In facter part 1 we showed how to use facter, as well as how to create custom facts using ruby and environment variables. In this post, we will go into further details how to test, distribute custom facts in your environment whether you are using puppet standalone or in master/agent mode. As a refresher, here's the ruby code for system_role.rb:

require 'facter'
Facter.add(:system_role) do
  setcode do
    Facter::Util::Resolution.exec("cat /etc/system_role")
  end
end

For testing, we need to ensure the system_role.rb file is in the RUBYLIB or FACTERLIB path. In the case of RUBYLIB, it will search for *.rb in a facter sub directory, and FACTERLIB will simply search for all *.rb files in it's path. We can test system_role after setting configuring either one of these environment variables.

$ export RUBYLIB=~/lib:$RUBYLIB

or

$ export FACTERLIB=~/lib/facter:$FACTERLIB
$ mv system_role.rb ~/lib/facter
$ facter system_role
webserver

To distribute custom facts from puppet master, enable pluginsync=true in puppet.conf [main] section. The puppet agent will retreive any new facts automatically:

$ puppetd --test --pluginsync
info: Retrieving plugin
notice: /File[/var/lib/puppet/lib/facter]/ensure: created
notice: /File[/var/lib/puppet/lib/facter/system_role.rb]/ensure: ...

By default the facts are stored in {$vardir}/facts, and it's configurable via the setting factpath also in puppet.conf [main] section:

$ puppetd --configprint factpath
/var/lib/puppet/facts/

Once a custom fact is distributed with pluginsync, it can be tested on the puppet agent with the --puppet flag without the need to configure RUBYLIB or FACTERLIB:

$ facter --puppet system_role
webserver

Similar to custom providers, the same fact can be implemented separately for different platforms and facter will execute the appropriate implementation based on the confine criteria. In the example below system_role file will be retrieved from /etc/sysconfig for CentOS and RedHat, and use /etc for all other cases:

require 'facter'
Facter.add(:system_role) do
  confine :operatingsystem => %w{CentOS, RedHat}
  setcode do
   Facter::Util::Resolution.exec("cat /etc/sysconfig/system_role")
  end
end

Facter.add(:system_role) do
  setcode do
   Facter::Util::Resolution.exec("cat /etc/system_role")
  end
end

Since facter is executed on the client, variables in the Puppet manifest is not available, but facter is able use values provided by any existing facts. For example uptime:

Facter["uptime"].value()

In the next post we will examine how to write a complex custom fact and tying together all this information.

Share via:
Posted in:

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.