puppet custom fact for facter

Cmag picture Cmag · Oct 5, 2012 · Viewed 8.4k times · Source

Trying to write a custom facter module:

Facter.add("something_status") do
  setcode do
    $string_to_parse = Facter::Util::Resolution.exec('somecommand --print ')
    ... do something to string
    return something-new
  end
end

Very new to ruby... what would be the proper syntax to do something with this?

Answer

Valor picture Valor · Oct 5, 2012

You are not far away from doing it.. you dont need $ before variable names and you should not end with return. Latest var on scope will be retrieved by Facter.

Below it's an example code that analyze uname output and returns a fact with a string about kernel version and ipv6 support of it.

It's just an example but it works, just tried it.

Facter.add("customer") do
  setcode do
    kernel_release = Facter::Util::Resolution.exec('/bin/uname -r')
    # Get version
    if kernel_release =~ /^3.2/
      answer = "Latest Kernel"  
    elsif kernel_release =~ /^3.0/
      answer = "3.0 Kernel"
    elsif kernel_release =~ /^2.6/
      answer = "Decent Kernel"
    else 
      answer = "Unknown Kernel"
    end
    if answer =~ /ipv6/       
      answer += " with IPV6 Support"
    else 
      answer += " without IPV6 Support"
    end
  end
end  

Good Luck!

Some useful links for you: Ruby Wikibooks Control Structures, more ruby info