Return from Chef recipe without rising an exception
The way to abort run of a recipe (e.g. due to some condition) is to use a 'return' statement. A thorough discussion appears in jtimberman's answer to a parallel StackOverflow question.
I would break up that recipe into two pieces and use include_recipe
inside an if statement. For example in cookbooks/foo/default.rb
:
some_condition = File.exists?("/etc/whatever") # Your specific check here
if some_condition
include_recipe "foo::conditional_bit"
end
Put your conditional recipe in cookbooks/foo/recipes/conditional_bit.rb
and add recipe[foo]
to the appropriate node's run list.
Note that the order of execution will be slightly different to if you had specified a ruby block. The condition will be checked during the "compile" phase of the chef-client run, not the "execute" phase. See the Anatomy of a Chef Run page for details.