Custom Puppet Facts

From Kyle's Wiki
Jump to: navigation, search

Custom Puppet Facts allow you to extend facter to include custom tidbits of information regarding your setup. In general these should be things that are not necessarily configurable about the system, they just reflect facts about it.

Examples

A slightly better dmidecode fact thing:

if FileTest.exists?("/usr/sbin/dmidecode")

  # Add remove things to query here
  query = { 'BIOS Information'              => [ 'Vendor:', 'Version:', 'Release Date:' ],
            'System Information'            => [ 'Manufacturer:', 'Product Name:' , 'Serial Number:', 'Version:' ],
            'Chassis Information'           => ['Type:', 'Height:'],
            'Processor Information'         => ['Version:', 'Max Speed:'],
            'Memory Controller Information' => ['Maximum Memory Module Size:', 'Maximum Total Memory Size:' ] }
  
  # Run dmidecode only once
  output=%x{/usr/sbin/dmidecode 2>/dev/null}
  
  query.each_pair do |key,v|
    v.each do |value|
      output.split("Handle").each do |line|
        if line =~ /#{key}/  and line =~ /#{value} (\w.*)\n*./
          result = $1
          tag = key.split(' ')[0] + "_" + value.chomp(':').to_s.gsub(" ","_")
          Facter.add(tag) do
            confine :kernel => :Linux
            setcode do
              result
            end
          end # end facter.add
        end # end if line match
      end # end output line slpit
    end # end v.each
  end # end query.each_pair
end #endif

Testing

Running facter by itself won't suck up your custom fact. You have to set the FACTERLIB to pick up on it. (A puppet run will pick up on it automatically)

FACTERLIB="/etc/puppet/modules/backuppc/lib/facter" facter backuppc_hosts

When troubleshooting, you can use the "Facter.debug" command to spit out debug lines. Like this:

Facter.debug "Debug: #{bla}"

Then you can

facter --debug

References

Personal tools
Namespaces

Variants
Actions
Efforts
Toolbox
Meta