I have a situation where I have three cookbooks, each with a template resource that writes to an /etc/hosts file.
Rather than overwriting, I would like to append:
- The first cookbook creates /etc/hosts file and writes lines 1,2,3.
- The second cookbook appends lines 4,5. etc.
What's the right way to handle this in Chef land?
You should better create a cookbook managing the file which generate it from attributes.
CookbookA/attributes/default.rb
CookbookA/recipes/genfile.rb
CookbookA/templates/default/hosts.erb
And then in your other cookbooks attributes files:
And have thoose cookbooks depends on CookbookA and in their recipe call
include_recipe "CookbookA::genfile.rb"
Using
<<
you append to the attribute instead of overwriting them.