# Seeking variable template help

**URL:** <https://discourse.chef.io/t/seeking-variable-template-help/6652>\
**Category:** Chef Infra (archive)\
**Created:** [June 11, 2015, 11:00pm UTC](https://discourse.chef.io/t/seeking-variable-template-help/6652 "2015-06-11T23:00:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jennifer\_Fountain](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jennifer_fountain/32/63_2.png) [@Jennifer\_Fountain](https://discourse.chef.io/u/Jennifer_Fountain)\
**Post date:** [June 11, 2015, 11:00pm UTC](https://discourse.chef.io/t/seeking-variable-template-help/6652/1 "2015-06-11T23:00:55Z")

</div>

Here is what I am trying to do:

\<% %w[zone1 zone1].each do |role| -%\>  
zone “\<%= role %\>.domain.com” in {  
type master;  
file “\<%= role %\>.domain.conf”;  
\<% if [:bind][role][:notify] != nil %\>  
notify yes;  
also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
\<% end %\>  
allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };

};  
\<% end %\>

This works as expected:  
\<% %w[zone1 zone1].each do |role| -%\>  
zone “\<%= role %\>.domain.com” in {  
type master;  
file “\<%= role %\>.domain.conf”;  
};  
\<% end %\>

This not so much:  
\<% if [:bind][role][:notify] != nil %\>  
notify yes;  
also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
\<% end %\>  
allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };

};

I have tried several different variations but chef\_zero is still throwing  
errors.

eg: _Chef::Mixin::Template::TemplateError (no implicit conversion of String  
into Integer) on line #68:_

Is this not possible? Thanks for any advice!

–

Jennifer Fountain  
DevOPS

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen2/32/42_2.png) [@AJ\_Christensen2](https://discourse.chef.io/u/AJ_Christensen2)\
**Post date:** [June 11, 2015, 11:03pm UTC](https://discourse.chef.io/t/seeking-variable-template-help/6652/2 "2015-06-11T23:03:50Z")

</div>

You're missing the `node[:bind][role]` etc. the node object allows you to  
access attributes there.

In general I'd recommend using the variables() parameter to the template  
resource to bind particular attributes to known variables in advance, then  
refer to those instance variables in the template, kind of as a known  
interface between the node, the recipe and the template itself. Means you  
can change the templates variables from one attribute to another, more  
readily.

cheers,

--aj

On Fri, Jun 12, 2015 at 11:01 AM Jennifer Fountain [jfountain@meetme.com](mailto:jfountain@meetme.com)  
wrote:

> Here is what I am trying to do:
> 
> \<% %w[zone1 zone1].each do |role| -%\>  
> zone "\<%= role %\>.domain.com" in {  
> type master;  
> file "\<%= role %\>.domain.conf";  
> \<% if [:bind][role][:notify] != nil %\>  
> notify yes;  
> also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
> \<% end %\>  
> allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };
> 
> };  
> \<% end %\>
> 
> This works as expected:  
> \<% %w[zone1 zone1].each do |role| -%\>  
> zone "\<%= role %\>.domain.com" in {  
> type master;  
> file "\<%= role %\>.domain.conf";  
> };  
> \<% end %\>
> 
> This not so much:  
> \<% if [:bind][role][:notify] != nil %\>  
> notify yes;  
> also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
> \<% end %\>  
> allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };
> 
> };
> 
> I have tried several different variations but chef\_zero is still throwing  
> errors.
> 
> eg: _Chef::Mixin::Template::TemplateError (no implicit conversion of  
> String into Integer) on line #68:_
> 
> Is this not possible? Thanks for any advice!
> 
> --
> 
> Jennifer Fountain  
> DevOPS

---

<div class="post-metadata">

**Author:** ![Jennifer\_Fountain](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jennifer_fountain/32/63_2.png) [@Jennifer\_Fountain](https://discourse.chef.io/u/Jennifer_Fountain)\
**Post date:** [June 12, 2015, 12:19am UTC](https://discourse.chef.io/t/seeking-variable-template-help/6652/3 "2015-06-12T00:19:26Z")

</div>

Ah Thanks! I cannot believe I missed that. I guess that's what happens when  
you look at a screen for a long time 😃 Thanks! works great now.

On Thu, Jun 11, 2015 at 7:03 PM, AJ Christensen \<aj@junglistheavy.industries

> wrote:

> You're missing the `node[:bind][role]` etc. the node object allows you to  
> access attributes there.
> 
> In general I'd recommend using the variables() parameter to the template  
> resource to bind particular attributes to known variables in advance, then  
> refer to those instance variables in the template, kind of as a known  
> interface between the node, the recipe and the template itself. Means you  
> can change the templates variables from one attribute to another, more  
> readily.
> 
> cheers,
> 
> --aj
> 
> On Fri, Jun 12, 2015 at 11:01 AM Jennifer Fountain [jfountain@meetme.com](mailto:jfountain@meetme.com)  
> wrote:
> 
> > Here is what I am trying to do:
> > 
> > \<% %w[zone1 zone1].each do |role| -%\>  
> > zone "\<%= role %\>.domain.com" in {  
> > type master;  
> > file "\<%= role %\>.domain.conf";  
> > \<% if [:bind][role][:notify] != nil %\>  
> > notify yes;  
> > also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
> > \<% end %\>  
> > allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };
> > 
> > };  
> > \<% end %\>
> > 
> > This works as expected:  
> > \<% %w[zone1 zone1].each do |role| -%\>  
> > zone "\<%= role %\>.domain.com" in {  
> > type master;  
> > file "\<%= role %\>.domain.conf";  
> > };  
> > \<% end %\>
> > 
> > This not so much:  
> > \<% if [:bind][role][:notify] != nil %\>  
> > notify yes;  
> > also-notify { [\<%= [:bind][role][:notify\_ip] %\>; };  
> > \<% end %\>  
> > allow-update { \<%= [:bind][role][:allow\_update\_ip] %\>; };
> > 
> > };
> > 
> > I have tried several different variations but chef\_zero is still throwing  
> > errors.
> > 
> > eg: _Chef::Mixin::Template::TemplateError (no implicit conversion of  
> > String into Integer) on line #68:_
> > 
> > Is this not possible? Thanks for any advice!
> > 
> > --
> > 
> > Jennifer Fountain  
> > DevOPS

--

Jennifer Fountain  
DevOPS
