Replacing a template in a wrapper cookbook

眉间皱痕 提交于 2019-12-02 18:38:50

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.

As an alternative to Dave's answer, you can also use chef-rewind.

https://github.com/bryanwb/chef-rewind

A quick usage example from the github repo

# file postgresql/recipes/server.rb

template "/var/pgsql/data/postgresql.conf" do
  source  "postgresql.conf.erb"
  owner "postgres"
end

# file my-postgresql/recipes/server.rb

chef_gem "chef-rewind"
require 'chef/rewind'

include_recipe "postgresql::server"
# my-postgresql.conf.erb located inside my-postgresql/templates/default/my-postgresql.conf.erb
rewind :template => "/var/pgsql/data/postgresql.conf" do
  source "my-postgresql.conf.erb"
  cookbook_name "my-postgresql"
end

Making your patches and merging with upstream is the recommended practice when using knife because knife does some of the git branch merging stuff automatically for you and you are able to keep track of what you initially changed.

Simply overwriting files in your wrapper cookbook is a practice I did not encounter earlier but looks interesting ^^ Disadvantage: you have to maintain and merge upstream changes into your modified template manually and that may be sometimes more work than letting git do most of the stuff for you.

Third way: rely on "cookbook shadowing" (deprecated) that works when you have direct control over which cookbooks will the end user will use: http://tickets.opscode.com/browse/CHEF-2308

with chef 12 you can use edit_resource

include_recipe 'communitycookbook'

edit_resource!(:template, '/etc/myapp.conf') do
  source 'other.erb'
  cookbook 'wrapper'
  variables.update(port: 8080)
end

more about that you can find here: https://coderanger.net/rewind/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!