# Render partial template only if the node-partial exists

**URL:** <https://discourse.chef.io/t/render-partial-template-only-if-the-node-partial-exists/5298>\
**Category:** Chef Infra (archive)\
**Created:** [May 12, 2014, 7:13pm UTC](https://discourse.chef.io/t/render-partial-template-only-if-the-node-partial-exists/5298 "2014-05-12T19:13:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kallen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallen/32/1049_2.png) [@Kallen](https://discourse.chef.io/u/Kallen)\
**Post date:** [May 12, 2014, 7:13pm UTC](https://discourse.chef.io/t/render-partial-template-only-if-the-node-partial-exists/5298/1 "2014-05-12T19:13:11Z")

</div>

Hiya,

How can I render a base template _plus_ a partial template only if the partial  
exists for node[‘fqdn’]? Is there a way to do this? The way I see it now, the  
only way I can do this is to create a per-fqdn erb files in my cookbook for  
every host, even if only a few of them actually need to render the partial. I’ll  
bet there’s a better way to do this, I just don’t see it yet.

Let’s say I have a bunch of mysql database hosts, and most of them can render  
their my.cnf following one standard. And let’s say sometimes I have a need to  
extend the standard by appending extra config lines for only some of the db  
hosts.

Here are examples of the rendered my.cnf I want, the first is for a db host that  
has no special config lines, the second for a db host that needs a lil extra:

[mysqld]  
server-id = 14050101  
report-host = [db14b.dev.example.com](http://db14b.dev.example.com)  
!include /etc/mysql/\_my.cnf\_m3.large  
!include /etc/mysql/\_my.cnf.global\_5.5

[mysqld]  
server-id = 14050701  
report-host = [db26b.dev.example.com](http://db26b.dev.example.com)  
!include /etc/mysql/\_my.cnf\_m3.large  
!include /etc/mysql/\_my.cnf.global\_5.5

# 

# special stuff for this host is below, brought to you by chef partial template:

[mysqld]  
expire\_logs\_days=5  
replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%

The template resource in the recipe:

template “/etc/mysql/my.cnf” do  
source "my.cnf.erb"  
owner "root"  
group "root"  
mode 00644  
variables :server\_id =\> node[‘tokopf-bar’][‘mysql’][‘server\_id’],  
:mysql\_version =\> node[‘tokopf-bar’][‘mysql’][‘mysql\_version’]  
end

The main template, my.cnf.erb:  
[mysqld]  
server-id = \<%= @server\_id %\>  
report-host = \<%= node[‘fqdn’] %\>  
!include /etc/mysql/_my.cnf_\<%= node[‘ec2’][‘instance\_type’] %\>  
!include /etc/mysql/_my.cnf.global_\<%= @mysql\_version %\>  
\<%= render @node[‘fqdn’] + “.mycnf-partial.erb” -%\>

cookbooks/tokopf-bar/templates/default/db14b.dev.example.com.mycnf-partial.erb is empty…

cookbooks/tokopf-bar/templates/default/db26b.dev.example.com.mycnf-partial.erb:

# 

# special stuff for this host is below, brought to you by chef partial template:

[mysqld]  
expire\_logs\_days=5  
replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%

Thanks!  
kallen

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [May 12, 2014, 7:26pm UTC](https://discourse.chef.io/t/render-partial-template-only-if-the-node-partial-exists/5298/2 "2014-05-12T19:26:40Z")

</div>

I don't think this is currently possible without a lot of extra work. The complex way would be to look at the cookbook metadata in the cookbook collection to see what template files exist. This is definitely not how templates are intended to be used at present though.

--Noah

On May 12, 2014, at 12:13 PM, [kallen@groknaut.net](mailto:kallen@groknaut.net) wrote:

> Hiya,
> 
> How can I render a base template _plus_ a partial template only if the partial  
> exists for node['fqdn']? Is there a way to do this? The way I see it now, the  
> only way I can do this is to create a per-fqdn erb files in my cookbook for  
> every host, even if only a few of them actually need to render the partial. I'll  
> bet there's a better way to do this, I just don't see it yet.
> 
> Let's say I have a bunch of mysql database hosts, and most of them can render  
> their my.cnf following one standard. And let's say sometimes I have a need to  
> extend the standard by appending extra config lines for only some of the db  
> hosts.
> 
> Here are examples of the rendered my.cnf I want, the first is for a db host that  
> has no special config lines, the second for a db host that needs a lil extra:
> 
> [mysqld]  
> server-id = 14050101  
> report-host = [db14b.dev.example.com](http://db14b.dev.example.com)  
> !include /etc/mysql/\_my.cnf\_m3.large  
> !include /etc/mysql/\_my.cnf.global\_5.5
> 
> [mysqld]  
> server-id = 14050701  
> report-host = [db26b.dev.example.com](http://db26b.dev.example.com)  
> !include /etc/mysql/\_my.cnf\_m3.large  
> !include /etc/mysql/\_my.cnf.global\_5.5
> 
> # 
> 
> # special stuff for this host is below, brought to you by chef partial template:
> 
> [mysqld]  
> expire\_logs\_days=5  
> replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%
> 
> The template resource in the recipe:
> 
> template "/etc/mysql/my.cnf" do  
> source "my.cnf.erb"  
> owner "root"  
> group "root"  
> mode 00644  
> variables :server\_id =\> node['tokopf-bar']['mysql']['server\_id'],  
> :mysql\_version =\> node['tokopf-bar']['mysql']['mysql\_version']  
> end
> 
> The main template, my.cnf.erb:  
> [mysqld]  
> server-id = \<%= @server\_id %\>  
> report-host = \<%= node['fqdn'] %\>  
> !include /etc/mysql/_my.cnf_\<%= node['ec2']['instance\_type'] %\>  
> !include /etc/mysql/_my.cnf.global_\<%= @mysql\_version %\>  
> \<%= render @node['fqdn'] + ".mycnf-partial.erb" -%\>
> 
> cookbooks/tokopf-bar/templates/default/db14b.dev.example.com.mycnf-partial.erb is empty...
> 
> cookbooks/tokopf-bar/templates/default/db26b.dev.example.com.mycnf-partial.erb:
> 
> # 
> 
> # special stuff for this host is below, brought to you by chef partial template:
> 
> [mysqld]  
> expire\_logs\_days=5  
> replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%
> 
> Thanks!  
> kallen

---

<div class="post-metadata">

**Author:** ![Kallen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallen/32/1049_2.png) [@Kallen](https://discourse.chef.io/u/Kallen)\
**Post date:** [May 13, 2014, 10:58pm UTC](https://discourse.chef.io/t/render-partial-template-only-if-the-node-partial-exists/5298/3 "2014-05-13T22:58:54Z")

</div>

OK.. I found a solution:

The main template, my.cnf.erb:  
[mysqld]  
server-id = \<%= @server\_id %\>  
report-host = \<%= node['fqdn'] %\>  
!include /etc/mysql/_my.cnf_\<%= node['ec2']['instance\_type'] %\>  
!include /etc/mysql/_my.cnf.global_\<%= @mysql\_version %\>  
!include /etc/mysql/\_my.cnf.instance\_specific\_overrides \<-- the new line

In the templates dir in the cookbook:  
$ cat templates/default/my.cnf.instance\_specific\_overrides.erb

# A placeholder for overrides for specific fqdn

$ cat templates/host-db26.dev.example.com/my.cnf.instance\_specific\_overrides.erb

# Overrides specific to \<%= node['fqdn'] %\>

[mysqld]  
expire\_logs\_days=5  
replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%

Two template resources in the recipe:

template "/etc/mysql/my.cnf" do  
source "my.cnf.erb"  
owner "root"  
group "root"  
mode 00644  
variables :server\_id =\> node['tokopf-bar']['mysql']['server\_id'],  
:mysql\_version =\> node['tokopf-bar']['mysql']['mysql\_version']  
end

template "#{node['tokopf-bar']['mysql']['conf\_dir']}/\_my.cnf.instance\_specific\_overrides" do  
source "my.cnf.instance\_specific\_overrides.erb"  
owner "root"  
group node['tokopf-bar']['mysql']['root\_group']  
mode 00644  
end

On a given node during a chef-client run, if this dir exists with this file in  
it, chef will lay down this file:

templates/host-FQDN/my.cnf.instance\_specific\_overrides.erb

Otherwise, chef will lay down the override file found in the default dir.

Either way, the resulting my.cnf is happy.

yay  
kallen

On Mon, 12 May 2014, Noah Kantrowitz wrote:

> I don't think this is currently possible without a lot of extra work. The complex way would be to look at the cookbook metadata in the cookbook collection to see what template files exist. This is definitely not how templates are intended to be used at present though.
> 
> --Noah
> 
> On May 12, 2014, at 12:13 PM, [kallen@groknaut.net](mailto:kallen@groknaut.net) wrote:
> 
> > Hiya,
> > 
> > How can I render a base template _plus_ a partial template only if the partial  
> > exists for node['fqdn']? Is there a way to do this? The way I see it now, the  
> > only way I can do this is to create a per-fqdn erb files in my cookbook for  
> > every host, even if only a few of them actually need to render the partial. I'll  
> > bet there's a better way to do this, I just don't see it yet.
> > 
> > Let's say I have a bunch of mysql database hosts, and most of them can render  
> > their my.cnf following one standard. And let's say sometimes I have a need to  
> > extend the standard by appending extra config lines for only some of the db  
> > hosts.
> > 
> > Here are examples of the rendered my.cnf I want, the first is for a db host that  
> > has no special config lines, the second for a db host that needs a lil extra:
> > 
> > [mysqld]  
> > server-id = 14050101  
> > report-host = [db14b.dev.example.com](http://db14b.dev.example.com)  
> > !include /etc/mysql/\_my.cnf\_m3.large  
> > !include /etc/mysql/\_my.cnf.global\_5.5
> > 
> > [mysqld]  
> > server-id = 14050701  
> > report-host = [db26b.dev.example.com](http://db26b.dev.example.com)  
> > !include /etc/mysql/\_my.cnf\_m3.large  
> > !include /etc/mysql/\_my.cnf.global\_5.5
> > 
> > # 
> > 
> > # special stuff for this host is below, brought to you by chef partial template:
> > 
> > [mysqld]  
> > expire\_logs\_days=5  
> > replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%
> > 
> > The template resource in the recipe:
> > 
> > template "/etc/mysql/my.cnf" do  
> > source "my.cnf.erb"  
> > owner "root"  
> > group "root"  
> > mode 00644  
> > variables :server\_id =\> node['tokopf-bar']['mysql']['server\_id'],  
> > :mysql\_version =\> node['tokopf-bar']['mysql']['mysql\_version']  
> > end
> > 
> > The main template, my.cnf.erb:  
> > [mysqld]  
> > server-id = \<%= @server\_id %\>  
> > report-host = \<%= node['fqdn'] %\>  
> > !include /etc/mysql/_my.cnf_\<%= node['ec2']['instance\_type'] %\>  
> > !include /etc/mysql/_my.cnf.global_\<%= @mysql\_version %\>  
> > \<%= render @node['fqdn'] + ".mycnf-partial.erb" -%\>
> > 
> > cookbooks/tokopf-bar/templates/default/db14b.dev.example.com.mycnf-partial.erb is empty...
> > 
> > cookbooks/tokopf-bar/templates/default/db26b.dev.example.com.mycnf-partial.erb:
> > 
> > # 
> > 
> > # special stuff for this host is below, brought to you by chef partial template:
> > 
> > [mysqld]  
> > expire\_logs\_days=5  
> > replicate-wild-ignore-table = foo\_bar.KBToyAndHobby%
> > 
> > Thanks!  
> > kallen
