Precedence for facts evaluation in facter/puppet

Published: by

  • Categories:

For whatever reason may be, you might have a specific fact defined in more than one modules. This is a bad idea but what if you did, in that case which fact will be evaluated first? I had the same question for me and wiki said nothing about it, the guys at mailing list didn't reply either so I opened up the source code to look it. I would save you the trouble of doing that and summarize my findings.

The function that evaluates the fact is load() which is defined in loader.rb file (which on a centos machine is at location /usr/lib/ruby/site_ruby/1.8/facter/util/).

To get the value for a fact, we have to do something like:

facter -p factname
  1. First of all, writing the argument as factname and FACTNAME mean the same as the code load.rb converts everything to lowercase.
  2. load_env(shortname) tried to get the value from the environment variables first. Environment variables are of the format facter_kernel, facter_operatingsystem etc & case is ignored while searching for environment variables. If the value is found, this value is set the fact value. If nothing is found in the environment variable, the other ways are used.
  3. .rb is added after the fact name & this file is searched in the search_path (typically /var/lib/puppet/lib/facter if you are using pluginsync). If the file is found, it's executed and the facter's value is set accordingly. If the file is evaluated successfully, the value of fact is set & the fact name is appended to theloaded array. (Note: The fact is only added to the array if it already doesn't exist in the array)
  4. The next thing puppet tries is to load all the files that are present in the search_path/factname directory. Now, if there is a duplicate found in this directory which is already added before in step 3, (which is why I did the whole exercise) the earlier fact value is not overwritten because this fact will already exist in theloaded array and I already said that only new entries are added.
  5. After the directory with factname is loaded, then comes the part where facter really tries hard to add the fact by loading all the files that are present in all the directories in the search_path. But, if the fact is already defined, this function returns immidiately, so no duplicate facts are set again.

Let me know by comments if you need anything more to ask.