# How to have more than 1 exec, with at least 1 exec should execute successfully?

Hi,

I am having a scenario where I have 3 IPs, but not sure which is the proper one.

So I want to execute some command with each of the IP. If the command is successful with 1st IP, it should not execute the command for other 2 IPs.

If the 1st one failed, it should try with 2nd IP, and so on.

If all of them failed only puppet run should fail, otherwise it should till last IP.

edit retag close merge delete

Sort by » oldest newest most voted

There are several different ways to solve that problem. But the way you have the problem laid out does not sound very 'declarative'. Puppet is about saying 'this is how the state should be' and then making it so. I'm assuming that your execs will be run every single puppet run?

Perhaps if you had a custom fact that could figure out which IP to use (based on some kind of context) and then use that command in your puppet run to achieve the desired effect?

Please please please don't do the following terrible things I am suggesting... find a way to actually make your puppet code declarative. Have an external service that manages passing the information off to some remote server, etc... and manage that service with puppet.

disclaimer out of the way, here are a few ways to do what you are trying to do, going from less bad to more bad

1) Create a script that does all of the things you are trying to do OR create a command in a single exec that will run each of the commands with some sort of reverse short circuit logic.

2) Is it a problem if you run all three commands? Is there an order to this madness? If not, then you could have three execs

3) you could have each command run and create a sentinel value, and have an extra exec at the end to clean it up. If your first command runs, and greps the output for an 'error' message, or even better yet, it runs and returns a 0 or nonzero, you could && that command with a 'touch > /tmp/operation.success' command. have all of your other execs use a creates => '/tmp/operation.success' parameter and set up the command ordering. Follow it up with either a file {'/tmp/operation.success': ensure => absent } or an exec{ 'rm /tmp/operation.success': } ordered after the above execs, assuming you need this thing to 'run' every single puppet run.

more

thanks Dary, even I don't want to the 'mode bad' thing, but since our application having some issue, we have to have a work around with 'more bad'... for your 2nd option, yes, it is a problem if we run all three commands as it will consume lots of time!

( 2017-01-12 14:42:00 -0500 )edit