Shared files/templates between cookbooks

5,032

The cookbook_file and template resources support a "cookbook" parameter that specifies which cookbook contains the source file. Then you could create a "commons" cookbook where those files live as a single entity. For example:

% cookbooks/commons
cookbooks/commons
|-- files
|   `-- default
|       `-- master.conf
`-- templates
    `-- default
        `-- general.conf.erb

Suppose you have two cookbooks, thing1 and thing2, and they both use these. The recipes might be:

# thing1/recipes/default.rb
cookbook_file "/etc/thing1/master.conf" do
  source "master.conf"
  cookbook "commons"
end

template "/etc/thing1/general.conf" do
  source "general.conf.erb"
  cookbook "commons"
end

# thing2/recipes/default.rb
cookbook_file "/etc/thing2/like_master_but_different.conf" do
  source "master.conf"
  cookbook "commons"
end

template "/etc/thing2/not_as_general_as_you_think.conf" do
  source "general.conf.erb"
  cookbook "commons"
end

However, I would ask why do you have duplication between different kinds of functionality in your cookbooks? That is, would this kind of thing be suitable for a custom lightweight resource/provider that you use?

Share:
5,032

Related videos on Youtube

gdurham
Author by

gdurham

Updated on September 18, 2022

Comments

  • gdurham
    gdurham over 1 year

    We have multiple cookbooks which reference the same files and templates and were wondering if there is a reasonable way to ensure all of these are the same file to ensure that none go out of date. Is it possible to have a single file/template referenced by multiple recipes/cookbooks? I thought about using symlinks, however this will not work for us as Git does not support them.

  • gdurham
    gdurham over 11 years
    Thank you for the example as well as the other solution. This is perfect!