Ask Your Question

Conditional execution based on environment variable

asked 2014-11-26 15:54:31 -0500

puppet-user gravatar image

I'm very new to puppet so forgive me if my question is stupid. I wrote a puppet script that will install 2 software packages on systems. I want to make the script install either package 1 or package 1 & 2 based on an environment variable. I have the following syntax but i'm getting an error

 if $env_variable == 1 {
      node default {class{'package1':}->class{'package2':}}

} elsif  
    $env_variable == 2 {
          node default {class{'package1':}

"Error: Could not parse for environment production: Classes, definitions, and nodes may only appear at toplevel or inside other classes at site.pp:14 on node"

The script is able to view the env variable (I'm able to get the value if I do notify $env_variable) and It's also working if I don't have the if block.

Is this syntax not correct? Is it even possible to do it this way or is there another way to implement this?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-11-26 16:31:22 -0500

GregLarkin gravatar image

Try placing a case statement inside of your default node definition and based on the value of $env_variable, declare the appropriate classes. That should work fine and follow the correct Puppet language syntax.

edit flag offensive delete link more


It works! Thank you!

puppet-user gravatar imagepuppet-user ( 2014-11-26 17:21:14 -0500 )edit

Excellent, glad to hear it. If you wouldn't mind checking off my answer as correct, I would greatly appreciate it. Cheers!

GregLarkin gravatar imageGregLarkin ( 2014-11-26 21:34:17 -0500 )edit

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: 2014-11-26 15:54:31 -0500

Seen: 526 times

Last updated: Nov 26 '14