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, On Jun 22, 12:13 pm, Thomas Bellman <[email protected]> wrote: > 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 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
