Templates and loops..

923 views
Skip to first unread message

Christopher Johnston

unread,
Nov 19, 2009, 10:22:45 AM11/19/09
to Puppet Users
How come this doesnt work? I write this in a normal ruby script it
works just fine.

<% for cpu in (0..processorcount) %>

I want to loop through the number of processor so a specific action
can be taken to generate content for a file. I get an error on the
master of:

bad value for range at /etc/puppet"

-Chris

Julian Simpson

unread,
Nov 23, 2009, 5:11:41 AM11/23/09
to puppet...@googlegroups.com
2009/11/19 Christopher Johnston <chjo...@gmail.com>:
Processorcount isn't resolved. What machine are you running it on?
What version of facter do you have?

>> processorcount=nil
=> nil
>> for cpu in (0..processorcount)
>> puts cpu
>> end
ArgumentError: bad value for range
from (irb):2
>>

Cheers

J.

Christopher Johnston

unread,
Nov 23, 2009, 10:33:50 AM11/23/09
to puppet...@googlegroups.com
Its definitely there.. I even put a <%= processorcount %> to make sure the variable is set.  

<% processorcount.to_i-1 %>
<% for cpu in (0..processorcount) %>

Facter version is 1.52

# facter  | grep proc
processor0 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
processor1 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
processor2 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
processor3 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
processorcount => 4



--

You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=.



Julian Simpson

unread,
Nov 23, 2009, 5:35:29 PM11/23/09
to puppet...@googlegroups.com
2009/11/23 Christopher Johnston <chjo...@gmail.com>:
> Its definitely there.. I even put a <%= processorcount %> to make sure the
> variable is set.
> <% processorcount.to_i-1 %>
> <% for cpu in (0..processorcount) %>
> Facter version is 1.52
> # facter  | grep proc
> processor0 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
> processor1 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
> processor2 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
> processor3 => Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz
> processorcount => 4

And the <%= processorcount %> snippet resolved to 4 in your template?

I'm stumped then. I thought you could do iteration with any ERB
template, not just in Rails.

J.

Christopher Johnston

unread,
Nov 23, 2009, 6:41:14 PM11/23/09
to puppet...@googlegroups.com
Yes it prints 4 directly into the file, but I cant put processorcount into the loop.  Works fine in ruby but erb fails.  Any ideas here on how to do this?  I need to be able to determine the number of processors on the systems so I can generate a configuration file (loop through each processor number).

-Chris


J.

Scott Smith

unread,
Nov 23, 2009, 8:21:16 PM11/23/09
to puppet...@googlegroups.com
Christopher Johnston wrote:
> Yes it prints 4 directly into the file, but I cant put processorcount
> into the loop. Works fine in ruby but erb fails. Any ideas here on how
> to do this? I need to be able to determine the number of processors on
> the systems so I can generate a configuration file (loop through each
> processor number).
>

Try initializing it first. I know, annoying. Check this out:

http://stackoverflow.com/questions/1338960/ruby-templates-how-to-pass-variables-into-inlined-erb

-scott

Ohad Levy

unread,
Nov 23, 2009, 9:17:30 PM11/23/09
to puppet...@googlegroups.com
your problems is that processorcount is a string, here are some examples that works:

<% processorcount.each do |cpu| -%>
  <%= cpu.to_i * 100 %>
  <%# more stuff -%>
<% end %>

<% for cpu in (0..processorcount.to_i) %>
  <%= cpu %>
<%end%>

<% processorcount.to_i.times do |cpu| %>
  <%= cpu %>
<%end%>

Ohad


Christopher Johnston

unread,
Nov 25, 2009, 11:23:28 AM11/25/09
to puppet...@googlegroups.com
ah!  I am still learning ruby here so appreciate the time spent to show me this.

Shouldn't this also work to subtract (1) from the integer?  Works in a ruby script but not here.

19 <% processorcount.to_i - 1 %>
20 <% for cpu in (0..processorcount.to_i) %>




For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Peter Meier

unread,
Nov 25, 2009, 11:30:18 AM11/25/09
to puppet...@googlegroups.com
> ah! I am still learning ruby here so appreciate the time spent to show me
> this.
>
> Shouldn't this also work to subtract (1) from the integer? Works in a ruby
> script but not here.
>
> 19 <% processorcount.to_i - 1 %>
> 20 <% for cpu in (0..processorcount.to_i) %>

you mean it still counts up to 20? because processorcount isn't reduced.

<% for cpu in (0..(processorcount.to_i-1)) %>

or what do you mean with it doesn't work?

cheers pete

Christopher Johnston

unread,
Nov 25, 2009, 11:32:16 AM11/25/09
to puppet...@googlegroups.com
Ignore me, I had a typo.  I had put the parentheses around (0..(processorcount.to_i-1)  but I forgot the closing ')'

-Chris

Ian Ward Comfort

unread,
Nov 23, 2009, 6:54:52 PM11/23/09
to puppet...@googlegroups.com
On 23 Nov 2009, at 3:41 PM, Christopher Johnston wrote:
> Yes it prints 4 directly into the file, but I cant put
> processorcount into the loop. Works fine in ruby but erb fails.
> Any ideas here on how to do this? I need to be able to determine
> the number of processors on the systems so I can generate a
> configuration file (loop through each processor number).

>> <% for cpu in (0..processorcount) %>

Try:

<% for cpu in (0 .. processorcount.to_i) %>

--
Ian Ward Comfort <icom...@rescomp.stanford.edu>
Systems Team Lead, Student Computing, Stanford University

br...@reductivelabs.com

unread,
Nov 23, 2009, 7:59:51 PM11/23/09
to puppet...@googlegroups.com
On Mon, Nov 23, 2009 at 3:41 PM, Christopher Johnston <chjo...@gmail.com> wrote:
Yes it prints 4 directly into the file, but I cant put processorcount into the loop.  Works fine in ruby but erb fails.  Any ideas here on how to do this?  I need to be able to determine the number of processors on the systems so I can generate a configuration file (loop through each processor number).

-Chris

Chris, 

Have you tried `for cpu in (0..processorcount.to_i)` ?  I'm wondering if it's possible `processorcount` is a String.  You might want to try using `<%= processorcount.inspect %>` to verify.

BTW, other, more conventional options for doing this type of loop in Ruby would be:
  
    <% processorcount.to_i.times do |cpu| %>
    <% end %>

or

    <% 0.upto(processorcount.to_i - 1) do |cpu| %>
    <% end %>

Cheers,
Bruce
--
Bruce Williams
Developer @ Reductive Labs, http://reductivelabs.com

Ian Ward Comfort

unread,
Nov 23, 2009, 3:03:58 PM11/23/09
to puppet...@googlegroups.com
On 23 Nov 2009, at 7:33 AM, Christopher Johnston wrote:
> Its definitely there.. I even put a <%= processorcount %> to make
> sure the variable is set.
>
> <% processorcount.to_i-1 %>
> <% for cpu in (0..processorcount) %>

Reply all
Reply to author
Forward
0 new messages