Reputation: 161
I have a template file in my chef cookbook named 'Test Template', In my Chef recipe I am sourcing and updating my template file to a file '/etc/run/mn.txt' as follows:
template 'Test Template' do
source 'run.config'
path /etc/run/mn.txt
end
I need to update this template file dynamically during the chef run before the above "template" resource is called. How can I edit the template file itself within my cookbook before the template resource is called? I need to do this because there are lines I am adding to the template file and if I add the lines afterward to the created file after this resource block, it will just get reverted back to the original template contents during the next chef-client run.
Upvotes: 0
Views: 334
Reputation: 13
Do you know the contents that you want to include in /etc/run/mn.txt
before the template resource is executed? If so, you can use the variables property (A Hash of variables that are passed into a Ruby template file) to push the contents into the /etc/run/mn.txt
file:
template 'Test Template' do
source 'run.config'
path /etc/run/mn.txt
variables(
line_1: 'something here',
line_2: node['some_attribute'],
line_3: a_var
)
end
And then in your run.config, you could add to the end something like:
<previous contents of run.config>
<%= @line_1 %>
<%= @line_2 %>
<%= @line_3 %>
(Or you could pass the variables parameter a hash, and do some sort of loop through the hash keys in your template.. to make it more dynamic)
Upvotes: 0