Iterating over attributes in a template


#1

This might be a fundamental misunderstand on my part, but I can’t
figure out how to iterate nested attributes in a template.

Here’s what I have so far:

Recipe
template "/tmp/config.xml do
source "config.xml.erb"
variables :interface => node[:config][:interface]
end

Attributes file:
default[:config] = {
:interface => {
:total_cards => “75”,
:relevant_results => “6”
}
}

Template:
<% @interface.each do |x| %>
<% end %>

The error I get:
Chef::Mixin::Template::TemplateError (You tried to set a nested key,
where the parent is not a hash-like object

Thoughts? I have been beating my head on this.

Thanks,
Landon


#2

you need to yield k,v when eaching a Hash

On 12 August 2010 09:55, Landon chef@lclark.net wrote:

This might be a fundamental misunderstand on my part, but I can’t
figure out how to iterate nested attributes in a template.

Here’s what I have so far:

Recipe
template "/tmp/config.xml do
source "config.xml.erb"
variables :interface => node[:config][:interface]
end

Attributes file:
default[:config] = {
:interface => {
:total_cards => “75”,
:relevant_results => “6”
}
}

Template:
<% @interface.each do |x| %>
<% end %>

The error I get:
Chef::Mixin::Template::TemplateError (You tried to set a nested key,
where the parent is not a hash-like object

Thoughts? I have been beating my head on this.

Thanks,
Landon


#3

Thanks AJ but I tried that first… I went to yielding to a single
value after the message said it wasn’t a hash like structure. Any
other thoughts?

On Wed, Aug 11, 2010 at 3:28 PM, AJ Christensen aj@junglist.gen.nz wrote:

you need to yield k,v when eaching a Hash

On 12 August 2010 09:55, Landon chef@lclark.net wrote:

This might be a fundamental misunderstand on my part, but I can’t
figure out how to iterate nested attributes in a template.

Here’s what I have so far:

Recipe
template "/tmp/config.xml do
source "config.xml.erb"
variables :interface => node[:config][:interface]
end

Attributes file:
default[:config] = {
:interface => {
:total_cards => “75”,
:relevant_results => “6”
}
}

Template:
<% @interface.each do |x| %>
<% end %>

The error I get:
Chef::Mixin::Template::TemplateError (You tried to set a nested key,
where the parent is not a hash-like object

Thoughts? I have been beating my head on this.

Thanks,
Landon


#4

Try turning it into a hash (node.foo.bar.to_hash) in your recipe where the
template context is evaluated.

I haven’t had any issue doing this without the .to_hash call… You can just
use ‘node’ inside templates, too, I believe, which’ll let you work around.

Regards,

AJ

On 12 August 2010 12:28, Landon chef@lclark.net wrote:

Thanks AJ but I tried that first… I went to yielding to a single
value after the message said it wasn’t a hash like structure. Any
other thoughts?

On Wed, Aug 11, 2010 at 3:28 PM, AJ Christensen aj@junglist.gen.nz
wrote:

you need to yield k,v when eaching a Hash

On 12 August 2010 09:55, Landon chef@lclark.net wrote:

This might be a fundamental misunderstand on my part, but I can’t
figure out how to iterate nested attributes in a template.

Here’s what I have so far:

Recipe
template "/tmp/config.xml do
source "config.xml.erb"
variables :interface => node[:config][:interface]
end

Attributes file:
default[:config] = {
:interface => {
:total_cards => “75”,
:relevant_results => “6”
}
}

Template:
<% @interface.each do |x| %>
<% end %>

The error I get:
Chef::Mixin::Template::TemplateError (You tried to set a nested key,
where the parent is not a hash-like object

Thoughts? I have been beating my head on this.

Thanks,
Landon