Select Page
NOTE: This is a static archive of an old blog, no interactions like search or categories are current.

Previously I blogged a custom fact that reads /etc/facts.txt to build up some custom facts for use in Puppet manifests, well I’ve since learned a thing or two about Ruby and have improved the code, new code below:


if File.exists?(“/etc/facts.txt”)
   File.open(“/etc/facts.txt”).each do |line|
      var = nil
      value = nil

      var = $1 and val = $2 if line =~ /^(.+)=(.+)$/

      if var != nil && val != nil
         Facter.add(var) do
            setcode { val }
         end
      end
   end
end

As I mentioned previously I knew the code was horrible and had a whole redundant loop in it but couldn’t get it going otherwise.  The big change is in choice of variable names to use inside the setcode, sees value must be a reserved word or something, so now the code is much cleaner.