Ask Your Question

Ensuring idempotency using exec?

asked 2015-07-03 22:54:49 -0500

R gravatar image

I have about 20 python scripts which I am invoking via exec. My question is how to ensure idempotency? One way I could think of is to touch a different file every time the one script is executed. But that would require us to maintain 20 different files. I was looking at "refreshonly" in exec, where the triggers only if there is an event (notify or subscribe). I am not clear on this refreshonly option. Could somebody shed some light on this?

Thanks in advance

edit retag flag offensive close merge delete


It's hard to know what the best way to solve your problem might be without knowing what the 20 python scripts do.

Alex Harvey gravatar imageAlex Harvey ( 2015-07-04 00:31:19 -0500 )edit

As a workaround, let each script create a file. Test for the existence of this file before running the script (unless, onlyif). Of course, other commands in each script might fail silently then.

sinned gravatar imagesinned ( 2015-07-07 03:14:26 -0500 )edit

@ sinned Thanks a lot yes that is exactly what I was thinking, but the drawback is I will have to maintain 20 different files. Do you know what the refreshonly option in exec does? I am not too clear from reading the doc.

R gravatar imageR ( 2015-07-07 19:17:03 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-07-08 14:56:56 -0500

cbarbour gravatar image

The best way to ensure idempotency with exec is to invert your approach. Rather than focusing on how exec should invoke your script, focus on what this script is intended to accomplish. Test for that using the onlyif, unless, creates, and refreshonly parameters.

Focusing on the test rather than the command to run is declarative approach to writing exec statements.

For example, if your script is responsible for managing security settings, start by writing unless tests for those settings. Remember that you can pass an array to unless (all conditions must pass.) In this case, your test declares that the security settings are required, and execs the script to correct them if they are not set correctly.

If your script is responsible for installing an application, use an unless test that checks to see what version of the application is currently installed. If the application is not installed, or is the wrong version, the installer will then be invoked. In this case, your exec resource declares that a specific version of the application should be installed, and runs the installer if it's not correct.

I generally recommend refreshonly in cases where your script is actually refreshing something. Refreshonly can be unreliable; if the exec resource fails, there's no guarantee it will re-attempt the next time Puppet runs. If you do use refreshonly, be sure that a notification event will always be generated until the refresh succeeds.

I hope this helps.

edit flag offensive delete link more


Thanks a lot it surely does help :-)