A collection of task oriented solutions in Puppet

 

Selective exec running

Challenge

Sometimes you have a more complicated prerequisite that must be satisfied before an exec should run.

Solution

class exec_onlyif {

  # run exec only if command in onlyif returns 0.
  exec { 'run_account_purger':
    command => '/usr/local/sbin/account_purger',
    onlyif  => 'grep -c old_account /etc/passwd',
  }

  # or run multiple commands - all must succeed for exec to run
  exec { 'run_account_purger':
    command => '/usr/local/sbin/account_purger',
    onlyif  =>  [
                  'grep -c old_account /etc/passwd',
                  'test -d /home/old_account/'
                ]
  }

}

Explanation

Sometimes a simple creates isn't enough to determine if an exec should run or not. Using onlyif allows you to use the return value of one or more commands as your execution criteria; run the exec on a return of 0. The test commands will use the same $PATH as the exec itself and can be simple or as complicated as required, or as extreme as your patience for escaping string quoting allows!

If your monitoring system of choice has checks that you can manually run on the command line, and nagios is a great example of this, you can often get quite complicated and comprehensive checks with no extra coding required.

See also