21

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?

Thanks

ddario
  • 491

2 Answers2

37

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"],
}
Pascal Schmiel
  • 1,798
  • 12
  • 18
0

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.

chicks
  • 3,915
  • 10
  • 29
  • 37