Question

I have a chef recipe that looks something like:

Chef::Log.info('step1')
# do stuff

Chef::Log.info('step2')
bash "do_wget" do
  code <<-EOF
  wget somefile
  EOF
end

Chef::Log.info('step3')
# do stuff

The wget takes a while but the logging ends up looking like

step1    
step2    
step3
bash script runs #indicates that the bash script from step 2 is running

Is there a way to prevent the logging from step3 until the bash script is done executing?

Was it helpful?

Solution

You should get acquainted with Anatomy of Chef Run. It has 2 stages: compiling and executing.

No action is taken on the resources in the recipes at compiling stage - each evaluated resource is just taken and put into the Resource Collection. Plain Ruby code outside of resources is evaluated, however.

In execution stage it actually evaluates Resource Collection. But by this moment all your logging messages are already printed out.

If you need to run ruby code (including Chef logging) on execution stage, there is a special resource for that Ruby Block

ruby_block "Add logging step3" do
  block do
    Chef::Log.info('step3')
  end
  action :create
end

Another way may be, is to actually execute resource in compiling stage:

bash "do_wget" do
  code "wget somefile"
  action :nothing
end.run_action :run

Action :nothing is set to avoid running this resource twice (once in every stage).

OTHER TIPS

You can use the log resource instead, which I understand runs at the execution stage as you require

log "step2" do
    :info
end

or simply

log "step 3"

http://docs.opscode.com/resource_log.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top