Template variables definition


#1

I have this configuration

nas_hosts = search(:node, 'run_list:role\[nas\]')
template "#{node['freeradius']['config_dir']}/clients.conf" do
  source "clients.conf.erb"
  owner  node['freeradius']['user']
  group  node['freeradius']['group']
  mode   00600
  variables({
                :hosts => nas_hosts}
                )
  notifies :restart, 'service[freeradius]'
end

clients.conf.erb

<% @hosts.each do |hostname,ipaddress| -%>
# <%= hostname %>

client <%= ipaddress %> {
secret = secret
}

<% end -%>

And I get

# node[se.xxxxxxx.com]

client {
secret = s3cr3t

What is wrong in variables definition?


#2

Search returns a node object you have to iterate over, something like this in your recipe should get the data to play with more easily before sending them to the template:

nas_search = search(:node, 'run_list:role\[nas\]')
nas_hosts = {}
nas_search.each do |nodeobject|
  nas_hosts[honstnames] << nodeobject.node.hostname
  nas_hosts[ips] << nodeobject.node.ipaddress
end

Of course there are many ways to skin the above, the important thing being you must iterate over the node object returned which has node objects as well as other things…


#3

Please, try the following modification in your recipe.


nas_hosts = {}
search(:node, 'run_list:role\[nas\]').each do |n|
  nas_hosts[n.node.hostname] = n.node.ipaddress
end

#4

Thanks for help.
For me worked this modification in template.

<% @hosts.each do |host| -%>
# <%= host[‘fqdn’] %>

client <%= host[‘ipaddress’] %> {
secret = secret
}

<% end -%>