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


#1

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://nepalonrails.tumblr.com


#2

Ohai!

maybe this is what you are looking for:

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 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-mailhttps://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://nepalonrails.tumblr.com


#3

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://nepalonrails.tumblr.com
Sent with Sparrow (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:
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)> 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).
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://nepalonrails.tumblr.com


#4

On Sat, Aug 11, 2012 at 12:56 PM, Sachin Sagar Rai 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://nepalonrails.tumblr.com
Sent with Sparrow