Ask Your Question
3

Is there a logical and/or for the conditional if/else evaluator?

asked 2013-10-21 12:25:48 -0500

typedeaf gravatar image

updated 2013-10-21 12:26:35 -0500

Is it possible to chain multiple conditions together with the if/else conditional using logical and/ors? If so, what is the syntax?

ex.

if $var == 'txt' or $var == 'text' {
    action1
}

None of the other evaluations really make sense, and I would like do define something in one place, not two.

ex defining in two places, the gross way.

if $var == 'text' { 
    action1
} elsif $var == 'txt' {
   action1
}

I could use regex, but it seems a bit overkill.

if $var1 =~ /(te|t)xt/ {
    action1
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
9

answered 2013-10-21 14:16:39 -0500

Ancillas gravatar image

updated 2013-10-21 14:16:46 -0500

$color = 'blue'
$sound = 'loud'

if ($color == 'red') or ($sound == 'quiet') {
  notify {'should never see this': }
}

if ($color == 'blue') or ($sound == 'quiet') {
  notify {'color matched': }
}

if ($color == 'red') or ($sound == 'loud') {
  notify {'sound matched': }
}

if ($color == 'blue') and ($sound == 'loud') {
  notify {'both matched': }
}
edit flag offensive delete link more

Comments

those doesn't work, is it a bug? 1 if ($::color != 'dirt') or ($::color' != blue') 2 if $::color != ('dirt' or 'blue')

3h4x gravatar image3h4x ( 2014-11-24 12:57:03 -0500 )edit

$::color is different than $color. $::color would be the top scope.

Ancillas gravatar imageAncillas ( 2014-11-26 00:56:28 -0500 )edit

doesn't really matter which scope I'm using, did you try that code?

3h4x gravatar image3h4x ( 2014-12-16 02:00:41 -0500 )edit

I did not because your code has a typo, and I don't know which version of Puppet you're using. The scope does matter if you're trying to implement my solution from above. I'm on a phone now, but I'll swing back tomorrow and take a closer look.

Ancillas gravatar imageAncillas ( 2014-12-16 02:54:01 -0500 )edit

yop, it's just misunederstaning as I did not do it on colors code but on my puppet "production" code. I just wrote $::colors because answer is about it but I mean strictly logical form of it. cheers

3h4x gravatar image3h4x ( 2014-12-16 03:24:20 -0500 )edit
0

answered 2014-05-13 17:35:09 -0500

updated 2014-05-13 17:36:02 -0500

Can I use multiple lines for the condition, like:

if ($color == 'red') or 
   ($sound == 'quiet') {
  notify {'should never see this': }
}
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

2 followers

Stats

Asked: 2013-10-21 12:25:48 -0500

Seen: 32,754 times

Last updated: May 13 '14