1
votes

I've got a question on how to best do this senario, I've got a number of exec commands which need to be run in this order

Exec['A'] -> Exec['B']-> if var = 1(Exec['x']) if var = 2(Exec['y']) if var = 3 (Exec['z]) -> Exec['C'] -> Exec['D']

Basically I have an Exec then I have if statements and then I need to call an exec.

At this moment I've done the following

Exec['A'] { before Exec['B'] } 
Exec['B'] { subscribe Exec['A'] }
if var = 1(Exec['x']) { before Exec['C'] }
if var = 2(Exec['y']) { before Exec['C'] }
if var = 3 (Exec['z]) { before Exec['C'] }
Exec['C'] { before Exec['D'] }
Exec['D']

I'm not sure if this code is idempotent, I just want to confirm with the community in case they see anything I may not be seeing. I have a feeling that the if statements could run before the Exec['A'] which I do not want.

3

3 Answers

1
votes

The syntax is off, but this does indeed work

Exec['A'] { before => Exec['B'] }
if $var == 1 {
  Exec['x'] { before => Exec['C'] }
}
else {
  Exec['y'] { before => Exec['C'] }
}

Note that this code assumes that all the exec resources A, B, C, x and y have already been declared without any respective before parameters.

0
votes

If you want x,y, and z to run after A, then add require => Exec['A'].

Also, it appears all of the execs will run with every puppet run. Add a onlyif, unless, creates, or refreshonly attribute.

0
votes

To automatically test idempotence of your script, you may be interested in this tool: https://github.com/citac/citac

Citac systematically executes your Puppet manifest in various configurations, different resource execution orders, and more. The generated test reports inform you about issues with non-idempotent resources, convergence-related issues, etc.

The tool uses Docker containers for execution, hence your system remains untouched while testing. State changes are tracked during execution of the Puppet script, and detailed test reports are generated.

Please feel free to provide feedback, pull requests, etc. Happy testing!