ruby - Writing file with multiple Chef cookbooks all targeting the same file -


i have situation have 3 cookbooks, each template resource writes /etc/hosts file.

rather overwriting, append:

  • the first cookbook creates /etc/hosts file , writes lines 1,2,3.
  • the second cookbook appends lines 4,5. etc.

what's right way handle in chef land?

you should better create cookbook managing file generate attributes.

cookbooka/attributes/default.rb

default['hosts']['lines'] = [] 

cookbooka/recipes/genfile.rb

template "/etc/hosts"   source "hosts.erb" end 

cookbooka/templates/default/hosts.erb

#file generated chef <% node['hosts']['lines'].each |l| %> <% l -%> <% end.unless node['hosts']['lines'].empty? %> 

and in other cookbooks attributes files:

default['hosts']['lines'] << ["first line","second line"] 

and have thoose cookbooks depends on cookbooka , in recipe call include_recipe "cookbooka::genfile.rb"

using << append attribute instead of overwriting them.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -