Ask Your Question

"require" ignore if "exec" succeed.

asked 2017-08-30 21:43:36 -0500

Sraw gravatar image
exec { "check_presence":
    command => "/bin/true",
    onlyif => '/usr/bin/test -e /path',

file {"/home/user/test.txt":
    ensure => file,
    require => Exec["check_presence"]

I can't figure out what's wrong with my script. I use puppet apply test.pp to run this script. But no matter /path exists or not, the file test.txt was created.

I was using puppet 3.4.3. Any help is appreciated.

edit retag flag offensive close merge delete


That is correct. With relationships you define resources' order _in_ _terms_ _of_ _time_. With Puppet, you can _not_ define a relationship “define resource X, only if resource Y executed”.

Kai Burghardt gravatar imageKai Burghardt ( 2017-08-31 04:34:46 -0500 )edit

@Kai Burghardt But could you check this answer, it says we can use onlyif in this way, and there are a lot of agreements in comment. I just wonder am I misunderstand this answer?

Sraw gravatar imageSraw ( 2017-08-31 20:12:13 -0500 )edit

Well, if so, then that's at least bad. That's bad style, you know, don't abandon Puppet's declarative paradigm.

Kai Burghardt gravatar imageKai Burghardt ( 2017-09-01 05:41:13 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-09-01 23:39:16 -0500

greynolds gravatar image


Not sure what you are looking to accomplish. If it's to allow the execution of the exec file to initiate once when a file does not exist, then don't execute when the file does exist, then this will help.

exec { "check_presence": command => "/bin/true" onlyif => '/bin/grep -c /home/user /home/user/test.txt && exit 1 || exit 0' }

The intent of the meta parameter {'onlyif}, in this case, is to check to see if the file exists or not.

scan the /home/user directory for /home/user/test.txt if return a ( 1 ) exit out of the exec if this condition is not met then create the file based on your code block instructions.

I hope this helps

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-08-30 21:43:36 -0500

Seen: 72 times

Last updated: Sep 01