rubypuppetfacter

Fact file was parsed but returned an empty data set


For my current module, I need to check if php version 5 or 7 is installed and created a fact for this. The fact file is stored in the modules directory in facts.d/packageversion.rb and has the following content:

#!/usr/bin/ruby
require 'facter'

Facter.add(:php_version) do
  setcode do
    if File.directory? '/etc/php5'
      5
    else
      if File.directory? '/etc/php7'
        7
      else
        0
      end
    end
  end
end

But I can't use it in my module. In Puppet agent log, i get this error:

Fact file /var/lib/puppet/facts.d/packageversion.rb was parsed but returned an empty data set

How can I solve this?


Solution

  • facts.d is the module directory for external facts. You could place this file into the external facts directory, but the expected output would need to be key-value pairs. This is not happening, so Puppet is not finding a data set for the fact. https://docs.puppet.com/facter/3.6/custom_facts.html#executable-facts-----unix

    You have written this fact as a custom fact and not an external fact. Therefore, it needs to be placed inside the lib/facter directory in your module instead. Then it will function correctly. I notice this important information seems to have been removed from the latest Facter documentation, which probably lends to your confusion.

    Also, consider using an elsif in your code for clarity and optimization:

    if File.directory? '/etc/php5'
      5
    elsif File.directory? '/etc/php7'
      7
    else
      0
    end