我想在另一个配方中包含基于条件分支的配方。 目前计划在执行Ruby .each迭代器的过程中。
opscode维基说关于include_recipe的以下内容,“…对于相同配方的后续调用include_recipe将不起作用。”
我假设这意味着在运行过程中可能不会多次使用相同的配方使用include_recipe 。 但是,如果在迭代器的执行过程中被调用,这是否适用?
谢谢!
更新说明:
所以,只是为了澄清。 recipe_1是主要的食谱。 在recipe_1中,我遍历节点上的应用程序,如果App_3在节点上,则我想要包含recipe_2,如果Application_2在节点上或recipe_3。 如果我在节点上同时拥有两个应用程序,那么我想要确保当下一次迭代开始为App_3运行时,包含recipe_2的第一次迭代不会包含在recipe_1中。 再一次,我对提到的文档保持警惕:包含配方的资源将按顺序插入,在调用include_recipe的位置。
你的怀疑是正确的。 include_recipe是在RunContext (厨师 – 客户端运行)中累积的。 下面粘贴的是由IncludeRecipe#load_recipe调用的IncludeRecipe#load_recipe 。
# Evaluates the recipe +recipe_name+. Used by DSL::IncludeRecipe def load_recipe(recipe_name) Chef::Log.debug("Loading Recipe #{recipe_name} via include_recipe") cookbook_name, recipe_short_name = Chef::Recipe.parse_recipe_name(recipe_name) if loaded_fully_qualified_recipe?(cookbook_name, recipe_short_name) Chef::Log.debug("I am not loading #{recipe_name}, because I have already seen it.") false else loaded_recipe(cookbook_name, recipe_short_name) cookbook = cookbook_collection[cookbook_name] cookbook.load_recipe(recipe_short_name, self) end end
你可以像使用include_recipe一样使用相同的配方(比如在一个迭代器中),如果你多次包含它,它不会产生任何错误。
文档试图说,而且我已经在实践中观察到,包含配方中的资源在配方首次包含时会更新一次。 随后对include_recipe调用不会更改节点。