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
  • 511
  • 1
  • 3
  • 12
  • 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. – Dennis Kaarsemaker Jun 19 '13 at 11:29

2 Answers2

35

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,738
  • 12
  • 17
  • 6
    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
  • @ddario Put the unique name (e.g. path) in the name of the exec -- so something like ``exec {"check for ${thing}": ... }'' and then it will be unique for each item. – David Gardner Mar 10 '15 at 16:03
  • 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
  • this works very well for me, but, puppet agent always reports a change has occurred. I have this:
    
    exec { 'mumble':
    
      path => [ 'foo','bar',],
    
    }->
    
    stuff { 'stuff':
    
    }
    
    
    – Paul M Nov 02 '16 at 13:57
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,793
  • 10
  • 27
  • 36