Jeff Storey
Jeff Storey

Reputation: 57192

chef logging of wget

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?

Upvotes: 7

Views: 4260

Answers (2)

KCD
KCD

Reputation: 10281

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

Upvotes: 3

Draco Ater
Draco Ater

Reputation: 21226

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).

Upvotes: 8

Related Questions