Iterating over an array for a set of resources


#1

Hi list -

I’m struggling a little bit with something - I’m trying to provision a
"demo" environment and we need to set up several “fake” users.

Part of this process is to fill in some data in a template then run a
script, but I feel like the loop just selects the last thing in the array:

orgs = [“Ermergerd”, “FOO”]

orgs.each do |org|
Chef::Log.info("Org is : " + org)
node.set[:org] = org
Chef::Log.info("Org after node set: " + node[:org])
end

Is there a way to do this? I’m not even seeing anything come out of that
loop :-/


#2

A better way would be - how can I user a property in a recipe to expand
tokens inside an erb file - or is that not possible?

On Tue, Oct 16, 2012 at 7:46 PM, Maven User maven.2.user@gmail.com wrote:

Hi list -

I’m struggling a little bit with something - I’m trying to provision a
"demo" environment and we need to set up several “fake” users.

Part of this process is to fill in some data in a template then run a
script, but I feel like the loop just selects the last thing in the array:

orgs = [“Ermergerd”, “FOO”]

orgs.each do |org|
Chef::Log.info("Org is : " + org)
node.set[:org] = org
Chef::Log.info("Org after node set: " + node[:org])
end

Is there a way to do this? I’m not even seeing anything come out of that
loop :-/


#3

You can pass any ruby data to your template via the variables option.
Erb templates support loops:

template “/tmp/foo” do
source "foo.erb"
variables( :org => org )
end


<% @org.each do |org| -%>
org is: <%= org %>
<% end %>


This is untested code i just wrote in the email, but the concept
should be communicated.




On Tue, Oct 16, 2012 at 5:52 PM, Maven User <maven.2.user@gmail.com> wrote:
> A better way would be - how can I user a property in a recipe to expand
> tokens inside an erb file - or is that not possible?
>
>
>
>
> On Tue, Oct 16, 2012 at 7:46 PM, Maven User <maven.2.user@gmail.com> wrote:
>>
>> Hi list -
>>
>> I'm struggling a little bit with something - I'm trying to provision a
>> "demo" environment and we need to set up several "fake" users.
>>
>> Part of this process is to fill in some data in a template then run a
>> script, but I feel like the loop just selects the last thing in the array:
>>
>> orgs = ["Ermergerd", "FOO"]
>>
>>
>> orgs.each do |org|
>>    Chef::Log.info("Org is : " + org)
>>    node.set[:org] = org
>>    Chef::Log.info("Org after node set: " + node[:org])
>> end
>>
>> Is there a way to do this?  I'm not even seeing anything come out of that
>> loop :-/
>
>

#4

I just got it working - and yeah, very similar.

I have to generate a given file several times so the loop is outside of the
template - but got it working.

Thanks again!

On Tue, Oct 16, 2012 at 9:09 PM, Jesse Nelson spheromak@gmail.com wrote:

You can pass any ruby data to your template via the variables option.
Erb templates support loops:

template “/tmp/foo” do
source "foo.erb"
variables( :org => org )
end


<% @org.each do |org| -%>
org is: <%= org %>
<% end %>


This is untested code i just wrote in the email, but the concept
should be communicated.




On Tue, Oct 16, 2012 at 5:52 PM, Maven User <maven.2.user@gmail.com>
wrote:
> A better way would be - how can I user a property in a recipe to expand
> tokens inside an erb file - or is that not possible?
>
>
>
>
> On Tue, Oct 16, 2012 at 7:46 PM, Maven User <maven.2.user@gmail.com>
wrote:
>>
>> Hi list -
>>
>> I'm struggling a little bit with something - I'm trying to provision a
>> "demo" environment and we need to set up several "fake" users.
>>
>> Part of this process is to fill in some data in a template then run a
>> script, but I feel like the loop just selects the last thing in the
array:
>>
>> orgs = ["Ermergerd", "FOO"]
>>
>>
>> orgs.each do |org|
>>    Chef::Log.info("Org is : " + org)
>>    node.set[:org] = org
>>    Chef::Log.info("Org after node set: " + node[:org])
>> end
>>
>> Is there a way to do this?  I'm not even seeing anything come out of
that
>> loop :-/
>
>