Adhitya Ganesan
Adhitya Ganesan

Reputation: 27

Chef just ignores return block

Chef noob alert!!!!

So, I have this code

ohai 'reload' do
  action :reload
end


if node['packages'].keys.include?('ffmpeg')
  return
end

I want the recipe to return control if 'ffmpeg' is installed

I didn't do any installation or config for Ohai. Chef zero run says the plugin path /etc/chef/ohai/plugins does not exist but it also says :

"- re-run ohai and merge results into node attributes" .. So I guess that works

But Chef just ignores the if block. Chef doesn't even mention it when I run chef zero with "info" level enabled.

I have tried another variation of it too

return if node['packages'].keys.include?('ffmpeg')

But the same results. Why is Chef so weird? Or does it seems so to be because I didn't bother learning ruby? Why do all Chef resources assume that everybody knows all the basics.

Upvotes: 0

Views: 105

Answers (1)

Brandon Miller
Brandon Miller

Reputation: 5065

You should familiarize yourself with the Chef Client Overview. Especially the order of the Reset node attributes phase, Compile the resource collection phase, and Converge the node phase.

Ohai information is reloaded every time before resources are compiled, during the reset phase. If the goal is to reload Ohai for your if statement then the Ohai resource you have here isn't need. That Ohai resource will run during the converge phase. The if block runs before that Ohai resource, during the compile phase. This also means that if statement will return during the compile phase and not during the chef client run.

As a general rule; resources run during convergence but any ruby code that's not in a ruby_block resource runs during compilation. This means any if statement is completed during compilation with the state of the node before the current client run.

Hopefully this helps clear up some confusion.

Upvotes: 1

Related Questions