A collection of task oriented solutions in Puppet

 

Don't run exec if $file exists

Challenge

You have an exec that creates a file or directory that should only run if it doesn't already exist.

Solution

class absent_file_exec {

  exec { 'create_needed_directory':
    command => '/bin/mkdir -p /tmp/needed/directory',
    creates => '/tmp/needed/directory'
  }

}

Explanation

This is such a common requirement that puppet has a single property that "does the right thing". As long as the file / directory specified in "creates" exists puppet won't run the exec. It doesn't even cause clutter in the logs!

See also