Reputation: 1
I am having trouble trying to get information from a default attributes file into a template using Chef. Currently, I have this:
# attributes/default.rb
default['environment']['extrahosts'] = [ 'hostname1:address1', 'hostname2:address2' ]
#recipes/default.rb
extra_hosts = node[:environment][:extrahosts]
...
...
template '/blahblah' do
source 'blahblah.erb'
variables( :extra_hosts => extra_hosts )
end
#templates/blahblah.erb
<% for @item in @extra_hosts %>
- <%= @item %>
<% end %>
Although this doesn't work. What do I add to my template to yield:
- hostname1:address1
- hostname2:address2
Upvotes: 0
Views: 2414
Reputation: 54249
The way you write a loop in Ruby is to use the each
method and a block.
<% @extra_hosts.each do |item| %>
- <%= item %>
<% end %>
Also note that the loop variable doesn't have the at sign because it isn't an instance variable.
Upvotes: 2