I'm trying to write a function in puppet that will do a fail if the passed directory path does not exist.

if File["/some/path"] always returns true, and if defined(File["/some/path"]) only returns true if the resource is defined in puppet, regardless of whether it actually exists.

Is there a way to do this with a simple if statement?


  • 2
    Remember: manifests get compiled on the master. They know nothing about the client except for the facts it submits. So either write a fact that has this info, or take a step back and tell us what you're actually trying to solve so we can come up with a better approach than an if statement. – Dennis Kaarsemaker Jun 19 '13 at 11:29

Workaround for this: use onlyif on an exec "test" and require it in your action you want to execute:

exec {"check_presence":
  command => '/bin/true',
  onlyif => '/usr/bin/test -e /path/must/be/available',

whatever {"foo...":
  require => Exec["check_presence"],
| improve this answer | |
  • 5
    Oh that is evil, I like it :-) – Dennis Kaarsemaker Jun 19 '13 at 11:35
  • 1
    Could be even simpler: exec{"test -e /path/must/be/available": } – Dennis Kaarsemaker Jun 19 '13 at 11:51
  • 1
    Thanks. This works great for a single dir check. However, I'm trying to validate an array of directories... If I add this to a function, I get a 'cannot redeclare' error. – ddario Jun 19 '13 at 12:07
  • 6
    Great idea, but this means that on every Puppet run there will be a "Exec[check_presence]/returns: executed successfully" notice, which is really annoying! Better would be: exec { 'check_presence': command => '/bin/false', unless => '/usr/bin/test -e /path/must/be/available', } This will do the same, but will only create a error/notice when the path really doesn't exists. – S0me0ne Aug 29 '14 at 15:47
  • 2
    Anything which causes the "require" to fail will give an error as the whatever has a failed dependency, whether using onlyif or unless... So technically it works but you'll get a lot of errors logged in your puppet logs... – David Gardner Mar 10 '15 at 16:05

I too was having difficulty figure out how to keep a module from running if a directory wasn't present. This is what I found that worked for me.

exec { 'module_name':
   command => "command to run with variables", # Double quotes for Variable interpolation
   path   => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:",
   onlyif => 'test -d /mydirectory',

Adding the path is what really did the trick for me. From some reason even if I added the path to the test command such as /bin/test it didn't seem to work right.

Hope this helps others who may be having the same issue.

| improve this answer | |

You could also use the built-in function find_file() for it ... it returns undef when the file/path does not exists and a string if it's exists.


| improve this answer | |
  • 3
    This will only run on the master, and it's for finding files in modules. – bodgit Oct 10 '19 at 9:13
  • Thanks, but you could also use it to find files on the OS by providing the full-path to it or not ? – Mike Oct 11 '19 at 5:53
  • 1
    Only on the machine where the catalog is being compiled, i.e. the master. – bodgit Oct 11 '19 at 8:47
  • yes, that's right. – Mike Oct 13 '19 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.