I have some code that sets node attributes based on a search. I then want to use those attributes in a template. It appears the template is compiling before my code that figures out the values. So it takes 2 runs of chef-client to get to the state I want
if !node['foo']
search(:node, "recipes:bla").each do |bla|
if bla['bla'] > node['foo']
node['foo'] = bla['bla']
end
end
end
template "/tm/foo" do
source "foo"
end
I've tried putting this code in the recipe before and after the template, and in the attributes file. I don't think search
has an action or else I could try using .run_action() on it.
Is there any way to get the value of node['foo']
to be set so that it will be used in the template?
EDIT: Clarification
cook_a/attributes/default.rb
default['cook_a']['val_1'] = node['someval']
cook_a/recipes/default.rb
template "/etc/cook_a.conf" do
source "cook_a.conf.erb"
end
cook_a/templates/default/cook_a.conf.erb
some_var = <% node['cook_a']['val_1'] %>
Now over in another cookbook I'm overriding that value
coob_b/recipes/default.rb
node.set['someval'] = "foo"
include "cook_a"
But now its too late to change node['cook_a']['val_1']
and so the template writes the original value of node['someval']
during the first run. During the second run I get the correct value.
I'm reluctant to be setting node['cook_a']['val_1']
as that name may change and I'm trying to abstract away from the details of cook_a.