Ask Your Question
1

Custom fact is always empty

asked 2013-06-22 13:25:27 -0600

updated 2013-06-22 13:25:57 -0600

I have the following Ruby file (medialibraryhost.rb) in my module's lib/facter directory:

Facter.add(:media_library_host) do
  confine :kernel => :linux
  setcode do
    Facter::Util::Resolution.exec('if [ -e "/etc/medialibrary.host" ] ; then cat /etc/medialibrary.host ; fi')
  end
end

Basically it reads host line from a file. But no matter what I put in that file whenever I try to use $::media_library_host fact in my manifests it is empty. Any clues?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2013-06-22 13:54:53 -0600

domcleal gravatar image

Facter's exec method is designed to run commands it can find in PATH, not arbitrary shell code. You could do this instead:

Facter::Util::Resolution.exec('cat /etc/medialibrary.host') if File.exist?('/etc/medialibrary.host')

Or better:

File.read('/etc/medialibrary.host') if File.exist?('/etc/medialibrary.host')

Or perhaps best, if you're exclusively on Facter 1.7, since it transparently handles the file being missing:

Facter::Util::FileRead.read('/etc/medialibrary.host')

(add require 'facter/util/file_read' to the top of your source file too)

edit flag offensive delete link more

Comments

Thanks it worked. Though eventually I ended up using this: File.open('/etc/medialibrary.host') {|f| f.readline.chomp} if File.exist?('/etc/medialibrary.host')

Andrey Adamovich gravatar imageAndrey Adamovich ( 2013-06-22 17:15:20 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-06-22 13:25:27 -0600

Seen: 600 times

Last updated: Jun 22 '13