Is it possible to Chef `include_recipe` and indicate `only_if`/not_if` condition?

include_recipe isn't a normal resources in Chef but a normal method. Because of that, it ignores the passed block and subsequently the only_if condition specified there.

Fortunately, there occurs to be a solution for this. flaccid user from #chef freenode channel suggested the following solution, which works perfectly.

this_node_is_shard_broker = node["hostname"].include? "node2"
include_recipe "cubrid" if this_node_is_shard_broker

The above will execute include_recipe only if the hostname of the current running node is node2, which is exactly what I wanted to achieve.


This works just fine:

include_recipe "foo" if node['bar'] == 'baz'

Tags:

Chef Infra