Writing a file with multiple chefs throughout the test, targeting the same file
I have a situation where I have three cookbooks, each containing a template resource that writes to the / etc / hosts file.
Instead of rewriting, I would like to add:
- The first cookbook creates the / etc / hosts file and writes lines 1,2,3.
- The second cookbook adds 4.5 lines. and etc.
What's the right way to handle this in the land of a chef?
+3
source to share
1 answer
You are better off creating a cook file that generates it from attributes.
CookbookA / attributes / default.rb
default['hosts']['lines'] = []
CookbookA / Recipes / genfile.rb
template "/etc/hosts" do
source "hosts.erb"
end
CookbookA / templates / default /hosts.erb
#File Generated by Chef
<% node['hosts']['lines'].each do |l| %>
<% l -%>
<% end.unless node['hosts']['lines'].empty? %>
And then in your other cookbooks attributes the files:
default['hosts']['lines'] << ["first line","second line"]
And thoose cookbooks depend on CookbookA and in their recipe include_recipe "CookbookA::genfile.rb"
By using <<
, you are adding the attribute instead of overwriting them.
+2
source to share