# How to add/modify the default template provided by cookbook but without editing the given template?

**URL:** <https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933>\
**Category:** Chef Infra (archive)\
**Created:** [July 19, 2012, 5:30am UTC](https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933 "2012-07-19T05:30:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![millisami\_r](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/millisami_r/32/418_2.png) [@millisami\_r](https://discourse.chef.io/u/millisami_r)\
**Post date:** [July 19, 2012, 5:30am UTC](https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933/1 "2012-07-19T05:30:38Z")

</div>

Ohai chefs!

e.g.

Following is the snippet from the ‘chef-client::config’ recipe.

```
template "#{node["chef_client"]["conf_dir"]}/client.rb" do
  source "client.rb.erb"
  owner "root"
  group root_group
  mode 0644
  variables :chef_node_name => chef_node_name
  notifies :create, "ruby_block[reload_client_config]"
end

```

Its values can be overridden to customize it.  
But what if I to edit the template itself or how to put the modified  
template in my cookbook to get rendered instead of editing the default  
template provided by the cookbook itself?

e.g.

Now I want to use `https://github.com/kisoku/chef-handler-mail`.  
For this to work beside downloading, uploading to chef-server and using in  
roles/other-recipes, I’ve to add

```
#Using /etc/chef/config.rb

require 'rubygems'
require 'chef/handler/mail'

mail_handler = MailHandler.new
report_handlers << mail_handler
exception_handlers << mail_handler

```

to the template itself.

Now how to just override the default template provided by chef-client at  
`templates/default/client.rb.erb` only with the additional  
chef-handler-mail code appended to it?

@millisami  
~Sachin Sagar Rai  
Ruby on Rails Developer  
[http://tfm.com.np](http://tfm.com.np)  
[http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Torben\_Knerr](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@Torben\_Knerr](https://discourse.chef.io/u/Torben_Knerr)\
**Post date:** [July 19, 2012, 7:41am UTC](https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933/2 "2012-07-19T07:41:15Z")

</div>

Ohai!

maybe this is what you are looking for:

> **[GitHub - chef-boneyard/chef\_handler: DEPRECATED: Development repository for...](https://github.com/chef-boneyard/chef_handler)**
>
> DEPRECATED: Development repository for Chef Cookbook chef\_handler - GitHub - chef-boneyard/chef\_handler: DEPRECATED: Development repository for Chef Cookbook chef\_handler

AFAICT the chef\_handler LWRP takes care of managing your handlers. I don't  
know whether it would write through to the client.rb though. See the  
example here:

chef\_gem "chef-handler-mail"  
gem "chef-handler-mail"

chef\_handler "MailHandler" do  
source 'chef/handler/mail'  
arguments :to\_address =\> "root"  
action :nothing  
end.run\_action(:enable)

I guess you would have to create a separate cookbook (e.g. 'my-handlers')  
where you define the handlers as shown above in the default recipe.

HTH,  
Torben

P.S.: It would be cool if you could manage the handlers via a databag -  
has someone done this already?

On Thu, Jul 19, 2012 at 7:30 AM, millisami r [millisami@gmail.com](mailto:millisami@gmail.com) wrote:

> Ohai chefs!
> 
> e.g.
> 
> Following is the snippet from the 'chef-client::config' recipe.
> 
> ```
> template "#{node["chef_client"]["conf_dir"]}/client.rb" do
> source "client.rb.erb"
> owner "root"
> group root_group
> mode 0644
> variables :chef_node_name => chef_node_name
> notifies :create, "ruby_block[reload_client_config]"
> end
> 
> ```
> 
> Its values can be overridden to customize it.  
> But what if I to edit the template itself or how to put the modified  
> template in my cookbook to get rendered instead of editing the default  
> template provided by the cookbook itself?
> 
> e.g.
> 
> Now I want to use `https://github.com/kisoku/chef-handler-mail`[https://github.com/kisoku/chef-handler-mail](https://github.com/kisoku/chef-handler-mail)  
> .  
> For this to work beside downloading, uploading to chef-server and using in  
> roles/other-recipes, I've to add
> 
> ```
> #Using /etc/chef/config.rb
> 
> require 'rubygems'
> require 'chef/handler/mail'
> 
> mail_handler = MailHandler.new
> report_handlers << mail_handler
> exception_handlers << mail_handler
> 
> ```
> 
> to the template itself.
> 
> Now how to just override the default template provided by chef-client at  
> `templates/default/client.rb.erb` only with the additional  
> chef-handler-mail code appended to it?
> 
> @millisami  
> ~Sachin Sagar Rai  
> Ruby on Rails Developer  
> [http://tfm.com.np](http://tfm.com.np)  
> [http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com)

---

<div class="post-metadata">

**Author:** ![millisami\_r](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/millisami_r/32/418_2.png) [@millisami\_r](https://discourse.chef.io/u/millisami_r)\
**Post date:** [August 11, 2012, 10:56am UTC](https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933/3 "2012-08-11T10:56:17Z")

</div>

Thanks for the reply.

Yup, agreed on using the LWRP given by chef\_handler cookbook.

But my question still remains that what if the third-party cookbook doesn't have the LWRP?

* * *

@millisami  
~ Sachin Sagar Rai  
Ruby on Rails Developer  
[http://tfm.com.np](http://tfm.com.np)  
[http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com)  
Sent with Sparrow ([http://www.sparrowmailapp.com/?sig](http://www.sparrowmailapp.com/?sig))

On Thursday, July 19, 2012 at 1:26 PM, Torben Knerr wrote:

> Ohai!
> 
> maybe this is what you are looking for:  
> [GitHub - chef-boneyard/chef\_handler: DEPRECATED: Development repository for Chef Cookbook chef\_handler](https://github.com/opscode-cookbooks/chef_handler)
> 
> AFAICT the chef\_handler LWRP takes care of managing your handlers. I don't know whether it would write through to the client.rb though. See the example here:  
> chef\_gem "chef-handler-mail" gem "chef-handler-mail" chef\_handler "MailHandler" do source 'chef/handler/mail' arguments :to\_address =\> "root" action :nothing end.run\_action(:enable)
> 
> I guess you would have to create a separate cookbook (e.g. 'my-handlers') where you define the handlers as shown above in the default recipe.
> 
> HTH,  
> Torben
> 
> P.S.: It would be cool if you could manage the handlers via a databag - has someone done this already?
> 
> On Thu, Jul 19, 2012 at 7:30 AM, millisami r \<[millisami@gmail.com](mailto:millisami@gmail.com) ([mailto:millisami@gmail.com](mailto:millisami@gmail.com))\> wrote:
> 
> > Ohai chefs!
> > 
> > e.g.
> > 
> > Following is the snippet from the 'chef-client::config' recipe.
> > 
> > ```
> > template "#{node["chef_client"]["conf_dir"]}/client.rb" do
> > source "client.rb.erb"
> > owner "root"
> > group root_group
> > mode 0644
> > variables :chef_node_name => chef_node_name
> > notifies :create, "ruby_block[reload_client_config]"
> > end
> > 
> > ```
> > 
> > Its values can be overridden to customize it.  
> > But what if I to edit the template itself or how to put the modified template in my cookbook to get rendered instead of editing the default template provided by the cookbook itself?
> > 
> > e.g.
> > 
> > Now I want to use `https://github.com/kisoku/chef-handler-mail` ([GitHub - kisoku/chef-handler-mail: send chef reports via email](https://github.com/kisoku/chef-handler-mail)).  
> > For this to work beside downloading, uploading to chef-server and using in roles/other-recipes, I've to add
> > 
> > ```
> > #Using /etc/chef/config.rb
> > 
> > require 'rubygems'
> > require 'chef/handler/mail'
> > 
> > mail_handler = MailHandler.new
> > report_handlers << mail_handler
> > exception_handlers << mail_handler
> > 
> > ```
> > 
> > to the template itself.
> > 
> > Now how to just override the default template provided by chef-client at `templates/default/client.rb.erb` only with the additional chef-handler-mail code appended to it?
> > 
> > @millisami  
> > ~Sachin Sagar Rai  
> > Ruby on Rails Developer  
> > [http://tfm.com.np](http://tfm.com.np)  
> > [http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Torben\_Knerr](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@Torben\_Knerr](https://discourse.chef.io/u/Torben_Knerr)\
**Post date:** [August 13, 2012, 7:21am UTC](https://discourse.chef.io/t/how-to-add-modify-the-default-template-provided-by-cookbook-but-without-editing-the-given-template/2933/4 "2012-08-13T07:21:01Z")

</div>

On Sat, Aug 11, 2012 at 12:56 PM, Sachin Sagar Rai [millisami@gmail.com](mailto:millisami@gmail.com) wrote:

> But my question still remains that what if the third-party cookbook doesn't  
> have the LWRP?

I believe you could place an "override template" in  
$CHEF\_REPO/site-cookbooks/chef-client/templates, which would be a  
rather hacky solution (think of monkeypatching the cookbook).

Actually, I would rather recommend to fork the chef-client cookbook  
and roll your own. If your addition is something generally useful you  
should try to get it back to the community (via COOK ticket + pull  
request), if it's very specific to your case you can stay with your  
own fork.

Cheers,  
Torben

> * * *
> 
> @millisami  
> ~ Sachin Sagar Rai
> 
> Ruby on Rails Developer  
> [http://tfm.com.np](http://tfm.com.np)  
> [http://nepalonrails.tumblr.com](http://nepalonrails.tumblr.com)  
> Sent with Sparrow
