Search code examples
chef-infralwrp

What happens when you assign a resource to a variable in a Chef recipe?


I'm looking at the johnbellone/consul-cookbook on github and I see the following:

install = consul_installation node['consul']['version'] do |r|
 if node['consul']['installation']
   node['consul']['installation'].each_pair { |k, v| r.send(k, v) }
 end
end

When it assigns the resource to the install variable does it ever execute the resource/provider? Maybe it executes it right away? Instead of doing the two-phase process of compiling and converging as separate phases?


Solution

  • It doesn't change anything, there is always a return value from the DSL methods just normally you ignore it so it gets thrown away. Specifically each DSL method returns the compiled resource object, in this case an instance of the ConsulInstallation class.

    This is being used in this case because of a pattern of mine for doing dependency inversion/injection for Chef resources. Later down you can see install.consul_program which is doing an inversion command lookup because the consul_installation resource has multiple underlying providers that can vary at runtime.