20

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

1
  • 3
    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. Jun 19, 2013 at 11:29

2 Answers 2

34

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"],
}
7
  • 6
    Oh that is evil, I like it :-) Jun 19, 2013 at 11:35
  • 1
    Could be even simpler: exec{"test -e /path/must/be/available": } Jun 19, 2013 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, 2013 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, 2014 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... Mar 10, 2015 at 16:05
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.

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.