Ask Your Question
2

Query on template multiple conditions

asked 2013-06-14 04:24:46 -0500

wingZero gravatar image

updated 2013-06-16 23:47:52 -0500

Ancillas gravatar image

Hi,

Is it possible for templates to have multiple if and/or statements.

It seems to work ok for 2, however any more than this and it does not seem to work correctly.

Details: Works for

<% if environment == "qa" and group == "test" %>

Does not work for

<% if environment == "qa" or environment == "dev" and group == "test" or group == "test2" %>
edit retag flag offensive close merge delete

Comments

I assume you mean the 2nd expression doesn't evaluate to true when expected, rather than throws a syntax error? What are the values of environment and group when you ...(more)

GregLarkin gravatar imageGregLarkin ( 2013-06-14 10:56:49 -0500 )edit

I'd like to see your Puppet code, and the values that are being used in the template, please.

Ancillas gravatar imageAncillas ( 2013-06-14 13:44:51 -0500 )edit

Hi, I am looking for it to be either environment qa/dev and then if the group matches either test or test2 for those 2 environments then the action is performed.

wingZero gravatar imagewingZero ( 2013-06-17 02:25:39 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-06-14 14:14:00 -0500

ramindk gravatar image

updated 2013-06-17 00:46:23 -0500

The code you're using should work correctly, tested with Ruby 1.8.7 and 1.9.3 in a script like the one below. However you may want to explicitly set the order in which it's evaluated in case your real code doesn't match the example. Here's a simple Ruby script, erb is just Ruby, to test with.

#!/usr/bin/env ruby

environment = 'dev'
group = 'test'

puts "My values are #{environment} #{group}"

if ((environment == 'qa' or environment == 'dev') and (group == 'test' or group == 'test2'))
  puts "I matched on both #{environment} #{group}"
end

# this is equivalent to ...
(more)
edit flag offensive delete link more

Comments

Does Ruby evaluation left to right instead of *and* before *or*?

Ancillas gravatar imageAncillas ( 2013-06-16 23:48:45 -0500 )edit

Made some edits to my answer to clarify. It does do and before at least in my testing. I think using parens in the real code might help track it down.

ramindk gravatar imageramindk ( 2013-06-17 00:49:00 -0500 )edit

Hi, I have retested with your addition of the brackets and this now seems to work as intended. Thanks very much for the response!!

wingZero gravatar imagewingZero ( 2013-06-17 03:06:48 -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

Stats

Asked: 2013-06-14 04:24:46 -0500

Seen: 2,452 times

Last updated: Jun 17 '13