erb templating support for case statements?

4,292 views
Skip to first unread message

CraftyTech

unread,
Jun 21, 2010, 11:07:30 AM6/21/10
to Puppet Users
Hello All,

Can you guys point out to me, how do I do a case statement within
a template? i.g: my.cnf

max_allowed_packet=<% case ($memorysize<=4) = 8M, case
($memorysize<=8) = 16M)?

I've tried different combinations, but so far no luck. The syntax
checker coughs up hair balls....

Thanks,

Henry

CraftyTech

unread,
Jun 21, 2010, 11:31:03 AM6/21/10
to Puppet Users
I'm trying this line on my my.cnf.erb file:

<% case memorysize when /<=4/ %> max_allowed_packet=8M <% case
memorysize when />4<=8/ %> max_allowed_packet=16M <%end%>

I got the syntax from a different post. still not working thought.

Joe McDonagh

unread,
Jun 21, 2010, 11:31:41 AM6/21/10
to puppet...@googlegroups.com
Anything inside <% %> uses standard ruby coding, however it does *not*
put stdout into the file. Take this for example:

<% if somevar == 100 -%>
variable="this"
<% else -%>
variable="that"
<% end -%>

This is how you would print out a setting for a variable.

--
--
Joe McDonagh
Operations Engineer
AIM: YoosingYoonickz
IRC: joe-mac on freenode
"When the going gets weird, the weird turn pro."

Benoit Cattié

unread,
Jun 21, 2010, 11:41:19 AM6/21/10
to puppet...@googlegroups.com, hmme...@gmail.com
CraftyTech a écrit, le 21/06/2010 17:07:
> Hello All,
>
Hey,

> Can you guys point out to me, how do I do a case statement within
> a template? i.g: my.cnf
>
> max_allowed_packet=<% case ($memorysize<=4) = 8M, case
> ($memorysize<=8) = 16M)?
>
I think case dont support "order" comparaison. You can do it with if / else.

Otherwise case statement is :
max_allowed_packet=<% case memorysize when 4 %>8M<% when 8 %>16M<% end %>

or

max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif
memorysize.to_i <= 8 %>16M<% end %>


> I've tried different combinations, but so far no luck. The syntax
> checker coughs up hair balls....
>
> Thanks,
>
> Henry
>
>

Benoit

CraftyTech

unread,
Jun 21, 2010, 3:37:37 PM6/21/10
to Puppet Users
Thanks for the response. Right now I have it:

max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif
memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16
%>32M<% elseif memorysize.to_i > 16 %>32M<%end %>

but for some reason ignores everything, and it just show
"max_allowed_packet=" with no value afterwards...

CraftyTech

unread,
Jun 22, 2010, 9:20:52 AM6/22/10
to Puppet Users
Can anyone help out, and let me know why the line below omits
everything after the "=" sign, and just puts out the
"max_allowed_packet="? It seems pretty straight forward, but for some
reason it doesn't work. Can anyone spot what I'm missing?

Thanks,

Henry

CraftyTech

unread,
Jun 22, 2010, 11:09:12 AM6/22/10
to Puppet Users
It works now... The syntax was right all along. I was testing it on a
VM which has less than a gig of mem, hence why it'd omit the logic,
since it didn't apply. Sometimes the hurtles are a lot closer than
you expect them to be :-)

Thomas Bellman

unread,
Jun 22, 2010, 12:13:16 PM6/22/10
to puppet...@googlegroups.com
On 2010-06-21 21:37, CraftyTech wrote:

> Thanks for the response. Right now I have it:
>
> max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif
> memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16
> %>32M<% elseif memorysize.to_i > 16 %>32M<%end %>
>
> but for some reason ignores everything, and it just show
> "max_allowed_packet=" with no value afterwards...

When I try that exact code in an ERB template, I get an error:

Failed to parse template /tmp/trh/smurf.erb: undefined method
`to_i=' for "1.93 GB":String at /tmp/trh/craftytech.pp:5 on
node kumiko.nsc.liu.se

There are actually several errors in your code. First of all, you
have a dollar sign in front of the first 'memorysize'. The dollar
signs are only valid syntax in Puppet manifests, not in ERB templates
(or other Ruby code, for that matter).

Second, in Ruby, it's not 'elseif', but 'elsif'.

Third, you are trying to assign to memorysize.to_i, by using the
= operator. Maybe you were thinking about using the equality
comparison operator (==)?

But even using the == operator would be wrong. To check if a value
lies inside a range, you must use the .include? method, like this:

(8.1 .. 16).include?(memorysize.to_i)

So, if I change the template to this:

max_allowed_packet=<%
if memorysize.to_i <= 4 %>8M<%

elsif (4.1 .. 8).include?(memorysize.to_i) %>16M<%
elsif (8.1 .. 16).include?(memorysize.to_i) %>32M<%
elsif memorysize.to_i > 16 %>64M<%
end %>

then it works. But since you are using .to_i, you might not get
quite what you expect slightly above each limit; if memorysize is
for example 4.99 GB, then memorysize.to_i will be 4, and you will
get "max_allowd_packet=8M" as result. Your use of 4.1 and 8.1
implies to me that you wanted to get "max_allowed_packet=16M" in
that case. Replacing .to_i with .to_f would fix that.

Also, since we are writing it as a chain of if-elsif-elsif statements,
this would be better, IMHO:

max_allowed_packet=<%
if memorysize.to_f <= 4 %>8M<%
elsif memorysize.to_f <= 8 %>16M<%
elsif memorysize.to_f <= 16 %>32M<%
else %>64M<%
end %>

However, the memorysize fact is unfortunately in a not very nice format.
It is a floating point number followed by a unit ("MB" or "GB" in most
common cases today, but can also be "kB" or "TB", or nothing at all to
signify bytes). Presumably you expect the memorysize to be expressed in
Gbytes, but if you run it on a machine with less than 1 Gbyte memory or
more that 1024 Gbyte memory, you will not get what you expect.

The correct way to handle this is to parse the unit and take that
into account. Here is how I would do this:

<% mem,unit = memorysize.split
mem = mem.to_f
# Normalize mem to Mebibyte
case unit
when nil: mem /= (1<<20)
when 'kB': mem /= (1<<10)
when 'MB': mem *= (1<<0)
when 'GB': mem *= (1<<10)
when 'TB': mem *= (1<<20)
end
-%>
max_allowed_packet=<%
if mem <= 4096 %>8M<%
elsif mem <= 8192 %>16M<%
elsif mem <= 16384 %>32M<%
else %>64M<%
end %>


/Bellman

CraftyTech

unread,
Jun 22, 2010, 3:49:25 PM6/22/10
to Puppet Users
Whoa dude... that's like a little crash course on ruby... not that I'm
complaining... :-))

You're right, that first "$" with the variable wasn't meant to be
there... and I was wondering the same thing regarding the usage of the
memorysize variable from facter. Thanks a lot Bellman, I now have a
good starting point...

Cheers,
Reply all
Reply to author
Forward
0 new messages