Should I use include_recipe or add the recipe to run_list?

43,901

Solution 1

As I see it, any recipe should be able to run on an empty machine on its own. So if some recipe A depends on recipe B run before it, I always use include_recipe.

For example: 2 cookbooks, tomcat and java. Tomcat requires java.

  1. When some user wants to install tomcat, he may have no idea that he actually requires some other cookbook to install it. He runs the tomcat recipe and either it fails with some completely unhelpful error message like "No java found" or even worse - it succeeds, but then of course the user cannot start tomcat, because he does not have java installed.

  2. But when there is a include_recipe 'java' line in tomcat cookbook, which also requires a depends 'java' line in metadata, the user when trying to install tomcat, will see the understandable error message: "the cookbook java not found". This way actually user can download dependencies on his own (or even with some automatic tool) without actually running recipes, but reading metadata.

Solution 2

All logic should be controlled with run lists. Cookbooks, try as they might, are not as re-usable as people would like to think. All include_recipe does is add another place where users have to look to figure out what the run list is going to do so make it explicit and put it in the run list.

Share:
43,901
Micah
Author by

Micah

Updated on July 05, 2022

Comments

  • Micah
    Micah almost 2 years

    Trying to figure out the best approach for a large project. When is it appropriate to add recipes within a recipe by using include_recipe as opposed to adding the recipe to the run_list? Is there a good rule of thumb?

  • Patrick M
    Patrick M over 10 years
    How intelligent is Chef about avoiding repetition of recipes? So if your cookbook depends on parent, you include_recipe parent and some unwitting soul adds parent to the run list, does parent::default.rb get run twice? If parent has been crafted correctly, nothing bad will happen to the machine, but how much time will it waste when you bootstrap/converge a new node? If Chef isn't automagic in dodging duplication, is there a recommended way to avoid it manually?
  • Draco Ater
    Draco Ater over 10 years
    It does not load the same recipe several times. It skips it, if it has seen it already.
  • Patrick M
    Patrick M over 10 years
    I suppose that's a benefit of idempotence: if you're guaranteed to have the same result regardless of how many times you run it, then you know you can skip it if it's already been run! Thanks Draco.
  • Kevin Meredith
    Kevin Meredith over 9 years
    does Chef immediately execute a recipe when it readsinclude_recipe foo?
  • Draco Ater
    Draco Ater over 9 years
    No, Chef run consists of 2 stages, first it reads all the recipes and constructs a collection of resources to manage, and only then executes the resources.
  • user1071847
    user1071847 about 7 years
    I would assume that one should use include_recipe to make a dependency explicit.