# Template variables definition

**URL:** <https://discourse.chef.io/t/template-variables-definition/10557>\
**Category:** Chef Infra (archive)\
**Created:** [March 10, 2017, 4:35pm UTC](https://discourse.chef.io/t/template-variables-definition/10557 "2017-03-10T16:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coul](https://avatars.discourse-cdn.com/v4/letter/c/b19c9b/32.png) [@coul](https://discourse.chef.io/u/coul)\
**Post date:** [March 10, 2017, 4:35pm UTC](https://discourse.chef.io/t/template-variables-definition/10557/1 "2017-03-10T16:35:14Z")

</div>

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](http://se.xxxxxxx.com)]

> client {  
> secret = s3cr3t

What is wrong in variables definition?

---

<div class="post-metadata">

**Author:** ![majormoses](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/majormoses/32/1999_2.png) [@majormoses](https://discourse.chef.io/u/majormoses)\
**Post date:** [March 11, 2017, 4:53pm UTC](https://discourse.chef.io/t/template-variables-definition/10557/2 "2017-03-11T16:53:11Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Aia](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@Aia](https://discourse.chef.io/u/Aia)\
**Post date:** [March 13, 2017, 4:22am UTC](https://discourse.chef.io/t/template-variables-definition/10557/3 "2017-03-13T04:22:14Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![coul](https://avatars.discourse-cdn.com/v4/letter/c/b19c9b/32.png) [@coul](https://discourse.chef.io/u/coul)\
**Post date:** [March 13, 2017, 11:26am UTC](https://discourse.chef.io/t/template-variables-definition/10557/4 "2017-03-13T11:26:09Z")

</div>

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

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

> client \<%= host['ipaddress'] %\> {  
> secret = secret  
> }

> \<% end -%\>
