Replacing a template in a wrapper cookbook

newmanne picture newmanne · Apr 10, 2013 · Viewed 11.5k times · Source

I'm trying to write a wrapper cookbook for the chef graphite repo

In the recipe carbon.rb, the following lines occur:

template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
  owner node['apache']['user']
  group node['apache']['group']
end

where in templates/default/storage-schemas.conf there is a storage-schemas.conf file that is not to my liking. I can edit the file inline and achieve what I want, but it doesn't seem like a good chef practice if I want to be able to keep my repo up to date without merge conflicts. So I was wondering if I could solve this with a wrapper cookbook.

My first though was something like

include_recipe "graphite"
template "#{node['graphite']['base_dir']}/conf/storage-schemas.conf" do
  owner node['apache']['user']
  group node['apache']['group']
end

where I would just rerun the command after the base recipe finished and put the file I wanted in wrappercookbook/templates/storage-schemas.conf.erb. Is this a common practice? It doesn't feel very DRY, but I can't think of a cleaner way.

Answer

Dave S. picture Dave S. · Apr 10, 2013

You're pretty close. Assuming you have a modified version of the storage-schemas.conf.erb file in your wrapper cookbook, you can just do:

include_recipe "graphite"
begin
  r = resources(:template => "#{node['graphite']['base_dir']}/conf/storage-schemas.conf")
  r.cookbook "my-cookbook"
rescue Chef::Exceptions::ResourceNotFound
  Chef::Log.warn "could not find template to override!"
end

You can also use a line like:

r.source "graphite-stuff/my-storage-schemas.conf.erb"

if you want to organize the files in your wrapper cookbook in a different way.