Pages

Friday, May 13, 2022

Puppet: using Facts in an Exec clause (onlyif or unless)

 According to this issue on Puppet labs, there's simply no way to use facts in an Puppet Exec clause for the purpose of onlyif or unless, as such:

exec {'some_exec':
  command => '/usr/bin/cmd',
  # this `onlyif` will cause your manifest to fail
  onlyif       => $::facts[some_fact] == true,
} 

However I found a simple workaround for this, simply set a var with your facts to either 'true' or 'false' (string values) and use those in your `onlyif` block

if $::facts[some_fact] == true {
  $exec_true = 'true'
}
else {$exec_true = 'false'}

then use that in your exec block:

exec {'some_exec':
  command => '/usr/bin/cmd',
  # literally calls '/usr/bin/{true,false}'
  onlyif       => "/usr/bin/${exec_true},
} 

No comments:

Post a Comment