Ask Your Question

Confine fact to specific command existance

asked 2017-09-22 01:53:38 -0500

Jhonny gravatar image

Is it possible to confine a custom fact to the existence of a specific command?

Please consider the question as it is laid down and ignore the fact that I used bash in the example.

Facter.add("bash_version") do # fact only runs if command (bash in this case) exists confine :true.which('bash') setcode do %x{bash --version | grep -E 'bash.*version' | cut -d ' ' -f 4 | cut -d '-' -f 1}.chomp end end

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-09-25 02:53:23 -0500

What you already had almost worked. When you're not confining to another Facter value, you can pass confine a block and put anything you want in there as long as it returns a truthy value (pretty much anything that isn't nil or false).

Facter.add(:bash_version) do
  confine { Facter::Core::Execution.which('bash') }

  setcode do
    Facter::Core::Execution.exec('bash --version')[/bash.*?version ([^-]+)/, 1]
edit flag offensive delete link more

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


Asked: 2017-09-22 01:53:38 -0500

Seen: 230 times

Last updated: Sep 25