主厨`include_recipe`是否可以指示`only_if`/not_if`条件?

Eye*_*Eye 11 chef-infra

我希望满足include_recipe only_if一些条件.以下代码不会引发任何错误,但它也不关心only_if条件,因此在任何情况下都会执行:

include_recipe "cubrid" do
    only_if "hostname | grep 'blahblahshouldnotmatch'"
end
Run Code Online (Sandbox Code Playgroud)

是否有可能include_recipe仅在某些条件下?

Eye*_*Eye 21

include_recipe不是Chef中的常规资源,而是一种常规方法.因此,它忽略了传递的块,随后忽略了only_if那里指定的条件.

幸运的是,有一个解决方案.弛缓从#chef freenode的信道用户建议如下解决方案,它完美地工作.

this_node_is_shard_broker = node["hostname"].include? "node2"
include_recipe "cubrid" if this_node_is_shard_broker
Run Code Online (Sandbox Code Playgroud)

include_recipe只有当前正在运行的节点的主机名是node2,这才会执行,这正是我想要实现的.

  • `include_recipe`似乎只是一个普通的ruby函数调用,因此你可以使用普通的ruby`if <condition> <code> end`. (8认同)

小智 7

这很好用:

include_recipe "foo" if node['bar'] == 'baz'
Run Code Online (Sandbox Code Playgroud)