1
votes

I'm trying to iterate through a hash defined in my Chef attributes file and write it to a config template:

default['disk'] = node['block_device'].select { |i,j| j['state'] == 'running' && i != 'cdrom' }.select { |r| puts "Disk #{r}"}

In my Chef template, I am calling the variable with <%= @disk %>, so all the work is being done in the attributes file variable.

The above attribute will show me the result I want when it is compiling the cookbook, but using the puts method will not write to the config template, and I come up with empty strings written instead (see below).

Compiling Cookbooks...
Disk sda
Disk sdb
Converging 7 resources
....
+    Disk "{}"

If I remove the puts method (should not need it to write to config template), then I get the entire ['block_device'] structure (instead of just the device name) as a Disk value written to config template instead.

I've also tried playing around with using the puts method within the config template as well, but got no where. How can I write a new line in my template per key value in the array during a chef-client run? I would like to get it written to the config template instead of STDOUT during compile??

1
What is a "hash array"? Do you mean a hash? - sawa

1 Answers

3
votes

Chef templates use Erb formatting do you would want to actually use that:

# recipe
template '/asdf' do
  # ...
  variables disks: node['block_device'].select { |i,j| j['state'] == 'running' && i != 'cdrom' }
end

# template
<%- @disk.each do |i, j| -%>
<%= i %>
<%- end -%>