0

I'm reading that a shell command can be executed from custom facts with Facter::Core::Execution.exec. I've made a fact with the following code:

Facter.add(:controller_id) do
  setcode do
    Facter::Core::Execution.exec('/usr/bin/jq -r .device._id /var/lib/mylib/system.json')
  end
end

When I run the command stand alone like /usr/bin/jq -r .device._id /var/lib/mylib/system.json on an agent it returns a string. But when I run the fact on my agent with puppet agent -t PuppetDb doesn't contain the new fact.

I can see that the agent sees the new fact code because it tells me my code has changed:

Notice: /File[/opt/puppetlabs/puppet/cache/lib/facter/controller_id.rb]/content: content changed '{md5}c3567db500497e3586617bfed072ca6d' to '{md5}bb617198c5612eee365b5af8d410d4bc'

But no error is returned telling me why the fact wasn't saved. Does anyone know what might be causing this issue?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
  • Try `\`/usr/bin/jq -r .device._id /var/lib/mylib/system.json\`` to return a string. – Matthew Schuchard May 12 '17 at 17:04
  • @MattSchuchard thanks, the fact is now being recorded, unlike before, but it is an empty string. Even though running the command normally from the agent it returns a string. – Philip Kirkbride May 12 '17 at 17:33
  • 2
    That command runs as root with no ENV when the fact is being collected. Would that cause an issue? Also, if you are you trying to parse information from a `json` file on the system, you can do that intrinsically with Ruby and not use a shell command. – Matthew Schuchard May 12 '17 at 18:23
  • @MattSchuchard thanks I was able to get what I needed done using ruby. I'm still curious as to whpy the jq program wouldn't run. I'll look into possible ENV issues on Monday. Cheers. – Philip Kirkbride May 12 '17 at 19:49
  • If the command writes its output to its standard error instead of its standard output then Facter probably will not capture it. In any case, you should consider writing this as an [external fact](https://docs.puppet.com/facter/3.6/custom_facts.html#external-facts) instead of a Ruby native fact, but at this point you'll need to clean the (non-working) Ruby fact from all client systems first. That won't directly solve your problem, but the facility is designed for cases just like this. – John Bollinger May 12 '17 at 20:29

0 Answers0