# Recipes execution order

**URL:** <https://discourse.chef.io/t/recipes-execution-order/5391>\
**Category:** Chef Infra (archive)\
**Created:** [June 10, 2014, 8:18am UTC](https://discourse.chef.io/t/recipes-execution-order/5391 "2014-06-10T08:18:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [June 10, 2014, 8:18am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/1 "2014-06-10T08:18:48Z")

</div>

Hello!!!

I’m trying to do some research on how evaluate chef recipes when they  
are executed.

For example:

I have a lot of recipes inside my Apache2 cookbook.

default.rb  
god\_monitor.rb  
iptables.rb  
logrotate.rb  
…  
…  
…

Is there a way to establish a weight for the recipes order??..

A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1

In this case is better to execute D than, B than, A than C.

Cheers!

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [June 10, 2014, 9:16am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/2 "2014-06-10T09:16:21Z")

</div>

On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

> Hello!!!
> 
> I'm trying to do some research on how evaluate chef recipes when they  
> are executed.
> 
> For example:
> 
> I have a lot of recipes inside my Apache2 cookbook.
> 
> default.rb  
> god\_monitor.rb  
> iptables.rb  
> logrotate.rb  
> ...  
> ...  
> ...
> 
> Is there a way to establish a weight for the recipes order??..
> 
> A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> 
> In this case is better to execute D than, B than, A than C.

Recipes execute in the order you tell them to. First the run list is recursively expanded so roles are replaced by their run list until you just have a list of recipes. Then each is executed in order. If, while executing, it hits an include\_recipe, then it executes that recipe at that point. All other file types where it loads all of them are executed is ASCII sort order.

--Noah

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [June 10, 2014, 9:51am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/3 "2014-06-10T09:51:16Z")

</div>

Perfect,

But in my case I'm trying to model something in chef in which the  
order of cookbooks or recipes can vary.

For example:

Using  
DB and WEB roles.  
Apache and MySQL cookbooks.  
A set of recipes for each cookbook.

Now i want to create a node within the DB and WEB roles.

I want to know if there are cases in which is better to apply the WEB  
then the DB roles recipes or DB first and then WEB.

Any idea of situations like this? In which the order of the execution  
can improve the system behavior (For example, the execution time for  
all recipes, or roles, or environments)?

Cheers!!!

On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:

> On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> 
> > Hello!!!
> > 
> > I'm trying to do some research on how evaluate chef recipes when they  
> > are executed.
> > 
> > For example:
> > 
> > I have a lot of recipes inside my Apache2 cookbook.
> > 
> > default.rb  
> > god\_monitor.rb  
> > iptables.rb  
> > logrotate.rb  
> > ...  
> > ...  
> > ...
> > 
> > Is there a way to establish a weight for the recipes order??..
> > 
> > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > 
> > In this case is better to execute D than, B than, A than C.
> 
> Recipes execute in the order you tell them to. First the run list is recursively expanded so roles are replaced by their run list until you just have a list of recipes. Then each is executed in order. If, while executing, it hits an include\_recipe, then it executes that recipe at that point. All other file types where it loads all of them are executed is ASCII sort order.
> 
> --Noah

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [June 10, 2014, 10:22am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/4 "2014-06-10T10:22:11Z")

</div>

Recipes and roles that you actually put in a run list should be self-contained. This means that they should both be order-independent and idempotent, so it shouldn't matter either way.

--Noah

On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

> Perfect,
> 
> But in my case I'm trying to model something in chef in which the  
> order of cookbooks or recipes can vary.
> 
> For example:
> 
> Using  
> DB and WEB roles.  
> Apache and MySQL cookbooks.  
> A set of recipes for each cookbook.
> 
> Now i want to create a node within the DB and WEB roles.
> 
> I want to know if there are cases in which is better to apply the WEB  
> then the DB roles recipes or DB first and then WEB.
> 
> Any idea of situations like this? In which the order of the execution  
> can improve the system behavior (For example, the execution time for  
> all recipes, or roles, or environments)?
> 
> Cheers!!!
> 
> On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:
> 
> > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> > 
> > > Hello!!!
> > > 
> > > I'm trying to do some research on how evaluate chef recipes when they  
> > > are executed.
> > > 
> > > For example:
> > > 
> > > I have a lot of recipes inside my Apache2 cookbook.
> > > 
> > > default.rb  
> > > god\_monitor.rb  
> > > iptables.rb  
> > > logrotate.rb  
> > > ...  
> > > ...  
> > > ...
> > > 
> > > Is there a way to establish a weight for the recipes order??..
> > > 
> > > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > > 
> > > In this case is better to execute D than, B than, A than C.
> > 
> > Recipes execute in the order you tell them to. First the run list is recursively expanded so roles are replaced by their run list until you just have a list of recipes. Then each is executed in order. If, while executing, it hits an include\_recipe, then it executes that recipe at that point. All other file types where it loads all of them are executed is ASCII sort order.
> > 
> > --Noah

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [June 10, 2014, 5:51pm UTC](https://discourse.chef.io/t/recipes-execution-order/5391/5 "2014-06-10T17:51:46Z")

</div>

Perfect, now ill try to change the answer.

Im modeling SPL's as part of my PhD course. Now i really like Chef and  
i wish to be able to model a condition in which the execution order of  
a Chef process (Calculating the recipes run list for example) can  
vary. If a i execute A firt then B or B first and then A.

Any idea? It will be an example of how to apply a formal semantics to  
calculate the best product. Im assuming that Chef is a SPL which  
generates valid configurations (Products) to be installed on a node.

Cheers!

On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:

> Recipes and roles that you actually put in a run list should be self-contained. This means that they should both be order-independent and idempotent, so it shouldn't matter either way.
> 
> --Noah
> 
> On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> 
> > Perfect,
> > 
> > But in my case I'm trying to model something in chef in which the  
> > order of cookbooks or recipes can vary.
> > 
> > For example:
> > 
> > Using  
> > DB and WEB roles.  
> > Apache and MySQL cookbooks.  
> > A set of recipes for each cookbook.
> > 
> > Now i want to create a node within the DB and WEB roles.
> > 
> > I want to know if there are cases in which is better to apply the WEB  
> > then the DB roles recipes or DB first and then WEB.
> > 
> > Any idea of situations like this? In which the order of the execution  
> > can improve the system behavior (For example, the execution time for  
> > all recipes, or roles, or environments)?
> > 
> > Cheers!!!
> > 
> > On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:
> > 
> > > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> > > 
> > > > Hello!!!
> > > > 
> > > > I'm trying to do some research on how evaluate chef recipes when they  
> > > > are executed.
> > > > 
> > > > For example:
> > > > 
> > > > I have a lot of recipes inside my Apache2 cookbook.
> > > > 
> > > > default.rb  
> > > > god\_monitor.rb  
> > > > iptables.rb  
> > > > logrotate.rb  
> > > > ...  
> > > > ...  
> > > > ...
> > > > 
> > > > Is there a way to establish a weight for the recipes order??..
> > > > 
> > > > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > > > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > > > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > > > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > > > 
> > > > In this case is better to execute D than, B than, A than C.
> > > 
> > > Recipes execute in the order you tell them to. First the run list is recursively expanded so roles are replaced by their run list until you just have a list of recipes. Then each is executed in order. If, while executing, it hits an include\_recipe, then it executes that recipe at that point. All other file types where it loads all of them are executed is ASCII sort order.
> > > 
> > > --Noah

---

<div class="post-metadata">

**Author:** ![Dan\_Razzell](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Dan\_Razzell](https://discourse.chef.io/u/Dan_Razzell)\
**Post date:** [June 10, 2014, 6:24pm UTC](https://discourse.chef.io/t/recipes-execution-order/5391/6 "2014-06-10T18:24:04Z")

</div>

Hi Carlos,

You can define a recipe which invokes _include\_recipe_ conditionally,  
thereby affecting the execution order, if that's truly all you need.  
But there are two things to watch out for:

1. If the same recipe is included multiple times, only the first  
inclusion will be processed.

2. The evaluation of recipes in Chef is done in the preprocessing stage,  
so it won't have access to  
values produced during subsequent resource processing.

If your algorithm needs to access dynamic values, you have to implement  
it in a resource such as _ruby\_block_ or in custom resources that you  
define. The Chef DSL is essentially a macro expansion, so any control  
structure such as conditionals or iteration in the recipes will be  
expanded to produce static text, which is then evaluated in the ordinary  
manner.

Cheers,  
Dan

[http://docs.opscode.com/chef/dsl\_recipe.html#include-recipes](http://docs.opscode.com/chef/dsl_recipe.html#include-recipes)

On 14-06-10 10:51 AM, Carlos Camacho wrote:

> Perfect, now ill try to change the answer.
> 
> Im modeling SPL's as part of my PhD course. Now i really like Chef and  
> i wish to be able to model a condition in which the execution order of  
> a Chef process (Calculating the recipes run list for example) can  
> vary. If a i execute A firt then B or B first and then A.
> 
> Any idea? It will be an example of how to apply a formal semantics to  
> calculate the best product. Im assuming that Chef is a SPL which  
> generates valid configurations (Products) to be installed on a node.
> 
> Cheers!
> 
> On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:
> 
> > Recipes and roles that you actually put in a run list should be self-contained. This means that they should both be order-independent and idempotent, so it shouldn't matter either way.
> > 
> > --Noah
> > 
> > On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> > 
> > > Perfect,
> > > 
> > > But in my case I'm trying to model something in chef in which the  
> > > order of cookbooks or recipes can vary.
> > > 
> > > For example:
> > > 
> > > Using  
> > > DB and WEB roles.  
> > > Apache and MySQL cookbooks.  
> > > A set of recipes for each cookbook.
> > > 
> > > Now i want to create a node within the DB and WEB roles.
> > > 
> > > I want to know if there are cases in which is better to apply the WEB  
> > > then the DB roles recipes or DB first and then WEB.
> > > 
> > > Any idea of situations like this? In which the order of the execution  
> > > can improve the system behavior (For example, the execution time for  
> > > all recipes, or roles, or environments)?
> > > 
> > > Cheers!!!
> > > 
> > > On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:
> > > 
> > > > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> > > > 
> > > > > Hello!!!
> > > > > 
> > > > > I'm trying to do some research on how evaluate chef recipes when they  
> > > > > are executed.
> > > > > 
> > > > > For example:
> > > > > 
> > > > > I have a lot of recipes inside my Apache2 cookbook.
> > > > > 
> > > > > default.rb  
> > > > > god\_monitor.rb  
> > > > > iptables.rb  
> > > > > logrotate.rb  
> > > > > ...  
> > > > > ...  
> > > > > ...
> > > > > 
> > > > > Is there a way to establish a weight for the recipes order??..
> > > > > 
> > > > > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > > > > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > > > > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > > > > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > > > > 
> > > > > In this case is better to execute D than, B than, A than C.  
> > > > > Recipes execute in the order you tell them to. First the run list is recursively expanded so roles are replaced by their run list until you just have a list of recipes. Then each is executed in order. If, while executing, it hits an include\_recipe, then it executes that recipe at that point. All other file types where it loads all of them are executed is ASCII sort order.
> > > > 
> > > > --Noah

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [June 11, 2014, 7:38am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/7 "2014-06-11T07:38:29Z")

</div>

You are right, maybe the recipes are not the most accurate approach to  
show an example of my research line  
([http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf](http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf))

I think Chef is not a good example for my research line, because i  
want to say things like this.

Execute cookbookA -\> cookbookB is bettter than execute cookbookB -\> cookbookA

Or

Execute recipeA -\> recipeB is better than execute recipeB -\> recipeA

Better (Run faster, Occupy less memory, etc..)

But i cant find any situation in which i can say things like this.  
If you have any idea they will be welcome!

Cheers!

On Tue, Jun 10, 2014 at 8:24 PM, Dan Razzell [danr@activestate.com](mailto:danr@activestate.com) wrote:

> Hi Carlos,
> 
> You can define a recipe which invokes include\_recipe conditionally, thereby  
> affecting the execution order, if that's truly all you need. But there are  
> two things to watch out for:
> 
> 1. If the same recipe is included multiple times, only the first inclusion  
> will be processed.
> 
> 2. The evaluation of recipes in Chef is done in the preprocessing stage, so  
> it won't have access to  
> values produced during subsequent resource processing.
> 
> If your algorithm needs to access dynamic values, you have to implement it  
> in a resource such as ruby\_block or in custom resources that you define.  
> The Chef DSL is essentially a macro expansion, so any control structure such  
> as conditionals or iteration in the recipes will be expanded to produce  
> static text, which is then evaluated in the ordinary manner.
> 
> Cheers,  
> Dan
> 
> [http://docs.opscode.com/chef/dsl\_recipe.html#include-recipes](http://docs.opscode.com/chef/dsl_recipe.html#include-recipes)
> 
> On 14-06-10 10:51 AM, Carlos Camacho wrote:
> 
> Perfect, now ill try to change the answer.
> 
> Im modeling SPL's as part of my PhD course. Now i really like Chef and  
> i wish to be able to model a condition in which the execution order of  
> a Chef process (Calculating the recipes run list for example) can  
> vary. If a i execute A firt then B or B first and then A.
> 
> Any idea? It will be an example of how to apply a formal semantics to  
> calculate the best product. Im assuming that Chef is a SPL which  
> generates valid configurations (Products) to be installed on a node.
> 
> Cheers!
> 
> On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> wrote:
> 
> Recipes and roles that you actually put in a run list should be  
> self-contained. This means that they should both be order-independent and  
> idempotent, so it shouldn't matter either way.
> 
> --Noah
> 
> On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> wrote:
> 
> Perfect,
> 
> But in my case I'm trying to model something in chef in which the  
> order of cookbooks or recipes can vary.
> 
> For example:
> 
> Using  
> DB and WEB roles.  
> Apache and MySQL cookbooks.  
> A set of recipes for each cookbook.
> 
> Now i want to create a node within the DB and WEB roles.
> 
> I want to know if there are cases in which is better to apply the WEB  
> then the DB roles recipes or DB first and then WEB.
> 
> Any idea of situations like this? In which the order of the execution  
> can improve the system behavior (For example, the execution time for  
> all recipes, or roles, or environments)?
> 
> Cheers!!!
> 
> On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> wrote:
> 
> On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> wrote:
> 
> Hello!!!
> 
> I'm trying to do some research on how evaluate chef recipes when they  
> are executed.
> 
> For example:
> 
> I have a lot of recipes inside my Apache2 cookbook.
> 
> default.rb  
> god\_monitor.rb  
> iptables.rb  
> logrotate.rb  
> ...  
> ...  
> ...
> 
> Is there a way to establish a weight for the recipes order??..
> 
> A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> 
> In this case is better to execute D than, B than, A than C.
> 
> Recipes execute in the order you tell them to. First the run list is  
> recursively expanded so roles are replaced by their run list until you just  
> have a list of recipes. Then each is executed in order. If, while executing,  
> it hits an include\_recipe, then it executes that recipe at that point. All  
> other file types where it loads all of them are executed is ASCII sort  
> order.
> 
> --Noah

---

<div class="post-metadata">

**Author:** ![coderanger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/coderanger/32/5_2.png) [@coderanger](https://discourse.chef.io/u/coderanger)\
**Post date:** [June 11, 2014, 7:45am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/8 "2014-06-11T07:45:05Z")

</div>

That kind of analysis is not performed at the level of recipes. Once a converge starts, it is pretty straight forward (on purpose) and effectively just executes linearly. That said, there is that kind of solver run at the level of cookbook versions. When chef-client starts up it figures out which cookbooks it needs based on the run list expansion, and then asks the chef-server to find the best set of versions for those. This takes in to account the inter-cookbook dependencies, environment constraints, and tries to use more recent versions when possible. The intent isn't to offer multiple convergence paths though, that seems too complex to hold a mental model of, and therefore exceptionally likely to break 🙂

--Noah

On Jun 11, 2014, at 12:38 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

> You are right, maybe the recipes are not the most accurate approach to  
> show an example of my research line  
> ([http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf](http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf))
> 
> I think Chef is not a good example for my research line, because i  
> want to say things like this.
> 
> Execute cookbookA -\> cookbookB is bettter than execute cookbookB -\> cookbookA
> 
> Or
> 
> Execute recipeA -\> recipeB is better than execute recipeB -\> recipeA
> 
> Better (Run faster, Occupy less memory, etc..)
> 
> But i cant find any situation in which i can say things like this.  
> If you have any idea they will be welcome!
> 
> Cheers!
> 
> On Tue, Jun 10, 2014 at 8:24 PM, Dan Razzell [danr@activestate.com](mailto:danr@activestate.com) wrote:
> 
> > Hi Carlos,
> > 
> > You can define a recipe which invokes include\_recipe conditionally, thereby  
> > affecting the execution order, if that's truly all you need. But there are  
> > two things to watch out for:
> > 
> > 1. If the same recipe is included multiple times, only the first inclusion  
> > will be processed.
> > 
> > 2. The evaluation of recipes in Chef is done in the preprocessing stage, so  
> > it won't have access to  
> > values produced during subsequent resource processing.
> > 
> > If your algorithm needs to access dynamic values, you have to implement it  
> > in a resource such as ruby\_block or in custom resources that you define.  
> > The Chef DSL is essentially a macro expansion, so any control structure such  
> > as conditionals or iteration in the recipes will be expanded to produce  
> > static text, which is then evaluated in the ordinary manner.
> > 
> > Cheers,  
> > Dan
> > 
> > [http://docs.opscode.com/chef/dsl\_recipe.html#include-recipes](http://docs.opscode.com/chef/dsl_recipe.html#include-recipes)
> > 
> > On 14-06-10 10:51 AM, Carlos Camacho wrote:
> > 
> > Perfect, now ill try to change the answer.
> > 
> > Im modeling SPL's as part of my PhD course. Now i really like Chef and  
> > i wish to be able to model a condition in which the execution order of  
> > a Chef process (Calculating the recipes run list for example) can  
> > vary. If a i execute A firt then B or B first and then A.
> > 
> > Any idea? It will be an example of how to apply a formal semantics to  
> > calculate the best product. Im assuming that Chef is a SPL which  
> > generates valid configurations (Products) to be installed on a node.
> > 
> > Cheers!
> > 
> > On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > wrote:
> > 
> > Recipes and roles that you actually put in a run list should be  
> > self-contained. This means that they should both be order-independent and  
> > idempotent, so it shouldn't matter either way.
> > 
> > --Noah
> > 
> > On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > wrote:
> > 
> > Perfect,
> > 
> > But in my case I'm trying to model something in chef in which the  
> > order of cookbooks or recipes can vary.
> > 
> > For example:
> > 
> > Using  
> > DB and WEB roles.  
> > Apache and MySQL cookbooks.  
> > A set of recipes for each cookbook.
> > 
> > Now i want to create a node within the DB and WEB roles.
> > 
> > I want to know if there are cases in which is better to apply the WEB  
> > then the DB roles recipes or DB first and then WEB.
> > 
> > Any idea of situations like this? In which the order of the execution  
> > can improve the system behavior (For example, the execution time for  
> > all recipes, or roles, or environments)?
> > 
> > Cheers!!!
> > 
> > On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > wrote:
> > 
> > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > wrote:
> > 
> > Hello!!!
> > 
> > I'm trying to do some research on how evaluate chef recipes when they  
> > are executed.
> > 
> > For example:
> > 
> > I have a lot of recipes inside my Apache2 cookbook.
> > 
> > default.rb  
> > god\_monitor.rb  
> > iptables.rb  
> > logrotate.rb  
> > ...  
> > ...  
> > ...
> > 
> > Is there a way to establish a weight for the recipes order??..
> > 
> > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > 
> > In this case is better to execute D than, B than, A than C.
> > 
> > Recipes execute in the order you tell them to. First the run list is  
> > recursively expanded so roles are replaced by their run list until you just  
> > have a list of recipes. Then each is executed in order. If, while executing,  
> > it hits an include\_recipe, then it executes that recipe at that point. All  
> > other file types where it loads all of them are executed is ASCII sort  
> > order.
> > 
> > --Noah

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [June 11, 2014, 10:30am UTC](https://discourse.chef.io/t/recipes-execution-order/5391/9 "2014-06-11T10:30:44Z")

</div>

Perfect i also thought that recipes are explored linearly as they were  
in the run list.

Can you tell me a case (if there is) in which when the chef-client  
figures out which cookbooks it needs. To decide for example, why  
choose cookbookA then cookbookB or choose cookbookB then cookbookA??  
But not using constraints or dependencies, i mean for example,  
choosing cookbookA then cookbookB is better because executing  
cookbookA first then cookbookB takes less time or overload less the  
system or something similar?

Cheers!

On Wed, Jun 11, 2014 at 9:45 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:

> That kind of analysis is not performed at the level of recipes. Once a converge starts, it is pretty straight forward (on purpose) and effectively just executes linearly. That said, there is that kind of solver run at the level of cookbook versions. When chef-client starts up it figures out which cookbooks it needs based on the run list expansion, and then asks the chef-server to find the best set of versions for those. This takes in to account the inter-cookbook dependencies, environment constraints, and tries to use more recent versions when possible. The intent isn't to offer multiple convergence paths though, that seems too complex to hold a mental model of, and therefore exceptionally likely to break 🙂
> 
> --Noah
> 
> On Jun 11, 2014, at 12:38 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> 
> > You are right, maybe the recipes are not the most accurate approach to  
> > show an example of my research line  
> > ([http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf](http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf))
> > 
> > I think Chef is not a good example for my research line, because i  
> > want to say things like this.
> > 
> > Execute cookbookA -\> cookbookB is bettter than execute cookbookB -\> cookbookA
> > 
> > Or
> > 
> > Execute recipeA -\> recipeB is better than execute recipeB -\> recipeA
> > 
> > Better (Run faster, Occupy less memory, etc..)
> > 
> > But i cant find any situation in which i can say things like this.  
> > If you have any idea they will be welcome!
> > 
> > Cheers!
> > 
> > On Tue, Jun 10, 2014 at 8:24 PM, Dan Razzell [danr@activestate.com](mailto:danr@activestate.com) wrote:
> > 
> > > Hi Carlos,
> > > 
> > > You can define a recipe which invokes include\_recipe conditionally, thereby  
> > > affecting the execution order, if that's truly all you need. But there are  
> > > two things to watch out for:
> > > 
> > > 1. If the same recipe is included multiple times, only the first inclusion  
> > > will be processed.
> > > 
> > > 2. The evaluation of recipes in Chef is done in the preprocessing stage, so  
> > > it won't have access to  
> > > values produced during subsequent resource processing.
> > > 
> > > If your algorithm needs to access dynamic values, you have to implement it  
> > > in a resource such as ruby\_block or in custom resources that you define.  
> > > The Chef DSL is essentially a macro expansion, so any control structure such  
> > > as conditionals or iteration in the recipes will be expanded to produce  
> > > static text, which is then evaluated in the ordinary manner.
> > > 
> > > Cheers,  
> > > Dan
> > > 
> > > [http://docs.opscode.com/chef/dsl\_recipe.html#include-recipes](http://docs.opscode.com/chef/dsl_recipe.html#include-recipes)
> > > 
> > > On 14-06-10 10:51 AM, Carlos Camacho wrote:
> > > 
> > > Perfect, now ill try to change the answer.
> > > 
> > > Im modeling SPL's as part of my PhD course. Now i really like Chef and  
> > > i wish to be able to model a condition in which the execution order of  
> > > a Chef process (Calculating the recipes run list for example) can  
> > > vary. If a i execute A firt then B or B first and then A.
> > > 
> > > Any idea? It will be an example of how to apply a formal semantics to  
> > > calculate the best product. Im assuming that Chef is a SPL which  
> > > generates valid configurations (Products) to be installed on a node.
> > > 
> > > Cheers!
> > > 
> > > On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > > wrote:
> > > 
> > > Recipes and roles that you actually put in a run list should be  
> > > self-contained. This means that they should both be order-independent and  
> > > idempotent, so it shouldn't matter either way.
> > > 
> > > --Noah
> > > 
> > > On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > > wrote:
> > > 
> > > Perfect,
> > > 
> > > But in my case I'm trying to model something in chef in which the  
> > > order of cookbooks or recipes can vary.
> > > 
> > > For example:
> > > 
> > > Using  
> > > DB and WEB roles.  
> > > Apache and MySQL cookbooks.  
> > > A set of recipes for each cookbook.
> > > 
> > > Now i want to create a node within the DB and WEB roles.
> > > 
> > > I want to know if there are cases in which is better to apply the WEB  
> > > then the DB roles recipes or DB first and then WEB.
> > > 
> > > Any idea of situations like this? In which the order of the execution  
> > > can improve the system behavior (For example, the execution time for  
> > > all recipes, or roles, or environments)?
> > > 
> > > Cheers!!!
> > > 
> > > On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > > wrote:
> > > 
> > > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > > wrote:
> > > 
> > > Hello!!!
> > > 
> > > I'm trying to do some research on how evaluate chef recipes when they  
> > > are executed.
> > > 
> > > For example:
> > > 
> > > I have a lot of recipes inside my Apache2 cookbook.
> > > 
> > > default.rb  
> > > god\_monitor.rb  
> > > iptables.rb  
> > > logrotate.rb  
> > > ...  
> > > ...  
> > > ...
> > > 
> > > Is there a way to establish a weight for the recipes order??..
> > > 
> > > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > > 
> > > In this case is better to execute D than, B than, A than C.
> > > 
> > > Recipes execute in the order you tell them to. First the run list is  
> > > recursively expanded so roles are replaced by their run list until you just  
> > > have a list of recipes. Then each is executed in order. If, while executing,  
> > > it hits an include\_recipe, then it executes that recipe at that point. All  
> > > other file types where it loads all of them are executed is ASCII sort  
> > > order.
> > > 
> > > --Noah

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [June 11, 2014, 5:12pm UTC](https://discourse.chef.io/t/recipes-execution-order/5391/10 "2014-06-11T17:12:31Z")

</div>

The Chef Client makes no choices like that -- it only runs things in  
the order you specify. It will not re-order things for you.

- Julian

On Wed, Jun 11, 2014 at 6:30 AM, Carlos Camacho  
[carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

> Perfect i also thought that recipes are explored linearly as they were  
> in the run list.
> 
> Can you tell me a case (if there is) in which when the chef-client  
> figures out which cookbooks it needs. To decide for example, why  
> choose cookbookA then cookbookB or choose cookbookB then cookbookA??  
> But not using constraints or dependencies, i mean for example,  
> choosing cookbookA then cookbookB is better because executing  
> cookbookA first then cookbookB takes less time or overload less the  
> system or something similar?
> 
> Cheers!
> 
> On Wed, Jun 11, 2014 at 9:45 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net) wrote:
> 
> > That kind of analysis is not performed at the level of recipes. Once a converge starts, it is pretty straight forward (on purpose) and effectively just executes linearly. That said, there is that kind of solver run at the level of cookbook versions. When chef-client starts up it figures out which cookbooks it needs based on the run list expansion, and then asks the chef-server to find the best set of versions for those. This takes in to account the inter-cookbook dependencies, environment constraints, and tries to use more recent versions when possible. The intent isn't to offer multiple convergence paths though, that seems too complex to hold a mental model of, and therefore exceptionally likely to break 🙂
> > 
> > --Noah
> > 
> > On Jun 11, 2014, at 12:38 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> > 
> > > You are right, maybe the recipes are not the most accurate approach to  
> > > show an example of my research line  
> > > ([http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf](http://simba.fdi.ucm.es/at/1-s2.0-S0950584913001262-main.pdf))
> > > 
> > > I think Chef is not a good example for my research line, because i  
> > > want to say things like this.
> > > 
> > > Execute cookbookA -\> cookbookB is bettter than execute cookbookB -\> cookbookA
> > > 
> > > Or
> > > 
> > > Execute recipeA -\> recipeB is better than execute recipeB -\> recipeA
> > > 
> > > Better (Run faster, Occupy less memory, etc..)
> > > 
> > > But i cant find any situation in which i can say things like this.  
> > > If you have any idea they will be welcome!
> > > 
> > > Cheers!
> > > 
> > > On Tue, Jun 10, 2014 at 8:24 PM, Dan Razzell [danr@activestate.com](mailto:danr@activestate.com) wrote:
> > > 
> > > > Hi Carlos,
> > > > 
> > > > You can define a recipe which invokes include\_recipe conditionally, thereby  
> > > > affecting the execution order, if that's truly all you need. But there are  
> > > > two things to watch out for:
> > > > 
> > > > 1. If the same recipe is included multiple times, only the first inclusion  
> > > > will be processed.
> > > > 
> > > > 2. The evaluation of recipes in Chef is done in the preprocessing stage, so  
> > > > it won't have access to  
> > > > values produced during subsequent resource processing.
> > > > 
> > > > If your algorithm needs to access dynamic values, you have to implement it  
> > > > in a resource such as ruby\_block or in custom resources that you define.  
> > > > The Chef DSL is essentially a macro expansion, so any control structure such  
> > > > as conditionals or iteration in the recipes will be expanded to produce  
> > > > static text, which is then evaluated in the ordinary manner.
> > > > 
> > > > Cheers,  
> > > > Dan
> > > > 
> > > > [http://docs.opscode.com/chef/dsl\_recipe.html#include-recipes](http://docs.opscode.com/chef/dsl_recipe.html#include-recipes)
> > > > 
> > > > On 14-06-10 10:51 AM, Carlos Camacho wrote:
> > > > 
> > > > Perfect, now ill try to change the answer.
> > > > 
> > > > Im modeling SPL's as part of my PhD course. Now i really like Chef and  
> > > > i wish to be able to model a condition in which the execution order of  
> > > > a Chef process (Calculating the recipes run list for example) can  
> > > > vary. If a i execute A firt then B or B first and then A.
> > > > 
> > > > Any idea? It will be an example of how to apply a formal semantics to  
> > > > calculate the best product. Im assuming that Chef is a SPL which  
> > > > generates valid configurations (Products) to be installed on a node.
> > > > 
> > > > Cheers!
> > > > 
> > > > On Tue, Jun 10, 2014 at 12:22 PM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > > > wrote:
> > > > 
> > > > Recipes and roles that you actually put in a run list should be  
> > > > self-contained. This means that they should both be order-independent and  
> > > > idempotent, so it shouldn't matter either way.
> > > > 
> > > > --Noah
> > > > 
> > > > On Jun 10, 2014, at 2:51 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > > > wrote:
> > > > 
> > > > Perfect,
> > > > 
> > > > But in my case I'm trying to model something in chef in which the  
> > > > order of cookbooks or recipes can vary.
> > > > 
> > > > For example:
> > > > 
> > > > Using  
> > > > DB and WEB roles.  
> > > > Apache and MySQL cookbooks.  
> > > > A set of recipes for each cookbook.
> > > > 
> > > > Now i want to create a node within the DB and WEB roles.
> > > > 
> > > > I want to know if there are cases in which is better to apply the WEB  
> > > > then the DB roles recipes or DB first and then WEB.
> > > > 
> > > > Any idea of situations like this? In which the order of the execution  
> > > > can improve the system behavior (For example, the execution time for  
> > > > all recipes, or roles, or environments)?
> > > > 
> > > > Cheers!!!
> > > > 
> > > > On Tue, Jun 10, 2014 at 11:16 AM, Noah Kantrowitz [noah@coderanger.net](mailto:noah@coderanger.net)  
> > > > wrote:
> > > > 
> > > > On Jun 10, 2014, at 1:18 AM, Carlos Camacho [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)  
> > > > wrote:
> > > > 
> > > > Hello!!!
> > > > 
> > > > I'm trying to do some research on how evaluate chef recipes when they  
> > > > are executed.
> > > > 
> > > > For example:
> > > > 
> > > > I have a lot of recipes inside my Apache2 cookbook.
> > > > 
> > > > default.rb  
> > > > god\_monitor.rb  
> > > > iptables.rb  
> > > > logrotate.rb  
> > > > ...  
> > > > ...  
> > > > ...
> > > > 
> > > > Is there a way to establish a weight for the recipes order??..
> > > > 
> > > > A) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 2.3  
> > > > B) default.rb -\> iptables.rb -\> god\_monitor.rb -\> logrotate.rb = 3.6  
> > > > C) logrotate.rb -\> default.rb -\> god\_monitor.rb -\> iptables.rb = 1  
> > > > D) default.rb -\> god\_monitor.rb -\> iptables.rb -\> logrotate.rb = 6.1
> > > > 
> > > > In this case is better to execute D than, B than, A than C.
> > > > 
> > > > Recipes execute in the order you tell them to. First the run list is  
> > > > recursively expanded so roles are replaced by their run list until you just  
> > > > have a list of recipes. Then each is executed in order. If, while executing,  
> > > > it hits an include\_recipe, then it executes that recipe at that point. All  
> > > > other file types where it loads all of them are executed is ASCII sort  
> > > > order.
> > > > 
> > > > --Noah

--  
[Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
[WWW: [http://www.aquezada.com/staff/julian](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
[gopher://sdf.org/1/users/keymaker/ \* compliant!]  
[PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]
