chef-infra

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


I want to include_recipe only_if some condition is met. The following code doesn't raise any error but it doesn't care about the only_if condition either, so gets executed in any cases:

include_recipe "cubrid" do
    only_if "hostname | grep 'blahblahshouldnotmatch'"
end

Is it possible to include_recipe only on some condition?


Solution

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