Search code examples
chef-infrachef-recipechef-solo

sequence of execution in Chef


I'm a bit new to chef. I'm just wondering if chef resources are like node-js.

For example,

recipe.rb

resource1 'foo' do
    property value
end

resource2 'bar' do
    property value
end

Consider that resource1 is a heavy weight process and resource2 is a light weight process. Is it possible that bar will be executed first and then foo in this case? Or will it be in sequence manner first foo will be executed and then bar.


Solution

  • Chef recipes are imperative, they will run in a sequence. The run list is also an array which will be run in order. Unless you define the same thing in the run list multiple times, then only the first will run.

    There are two stages to a chef run though. A compile phase, to organise what resources need to be run and resolve all variables. Then a run phase where each resource is actually executed. You can write recipes that trigger actions during the compile phase which may make it seem out of order.

    For example, the chef_gem resource is designed to do things at the compile stage so the gems are available during the run phase. The compile_time option can be used to turn this off.

    For a functional example...

    ruby_block "later" do
      block do
        Chef::Log.info "I am first in the file"
      end
    end
    
    Chef::Log.info "I am second in the file"
    

    The ruby block is a resource that is compiled for later execution.
    The ruby code is executed during compilation, so comes out first.