# Injecting a resource into another cookbook's recipe

**URL:** <https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833>\
**Category:** Chef Infra (archive)\
**Created:** [April 11, 2013, 7:07pm UTC](https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833 "2013-04-11T19:07:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Nuckolls](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kevin_nuckolls/32/327_2.png) [@Kevin\_Nuckolls](https://discourse.chef.io/u/Kevin_Nuckolls)\
**Post date:** [April 11, 2013, 7:07pm UTC](https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833/1 "2013-04-11T19:07:17Z")

</div>

Since it’s ruby, I assume it’s possible to have a resource run within  
another scope as defined at runtime. I basically ran into a situation where  
I defined a variable that I used to inject information in a template in a  
library cookbook, then found that I also wanted access to that variable  
from the wrapper cookbook.

I could probably put it in a library, but at this point I’m just curious if  
anyone has done this. Can you run a resource in another cookbook’s recipe  
if you even wanted to?

Not saying this is the correct way to do it, just curious if it’s actually  
possible.

-Kevin

---

<div class="post-metadata">

**Author:** ![steve](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/steve/32/294_2.png) [@steve](https://discourse.chef.io/u/steve)\
**Post date:** [April 15, 2013, 6:43pm UTC](https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833/2 "2013-04-15T18:43:34Z")

</div>

I've run defines and used LWRPs in other recipes -- seems to work, as long  
as the defining cookbook is loaded up earlier in the run list. However, I  
don't like the idea of leaving it up to the user to figure out whether or  
not they need to add some\_random\_cookbook to a run list, so I've adopted  
the pattern of "include\_recipe 'some\_random\_cookbook::default'" and simply  
not putting anything of substance in a default recipe that isn't required  
across the entire cookbook.

On Thu, Apr 11, 2013 at 12:07 PM, Kevin Nuckolls  
[kevin.nuckolls@gmail.com](mailto:kevin.nuckolls@gmail.com)wrote:

> Since it's ruby, I assume it's possible to have a resource run within  
> another scope as defined at runtime. I basically ran into a situation where  
> I defined a variable that I used to inject information in a template in a  
> library cookbook, then found that I also wanted access to that variable  
> from the wrapper cookbook.
> 
> I could probably put it in a library, but at this point I'm just curious  
> if anyone has done this. Can you run a resource in another cookbook's  
> recipe if you even wanted to?
> 
> Not saying this is the correct way to do it, just curious if it's actually  
> possible.
> 
> -Kevin

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen/32/525_2.png) [@AJ\_Christensen](https://discourse.chef.io/u/AJ_Christensen)\
**Post date:** [April 15, 2013, 7:41pm UTC](https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833/3 "2013-04-15T19:41:18Z")

</div>

A member of Heavy Water Operations (Chris Roberts, spox) just build a  
system that allows arbitrary injection of resources at any time in the  
recipe life cycle. Chris had noticed a pattern used like this in the  
AWS OpsWorks tool and set out to re-implement.

Check it out. I'll endeavor to pass any feedback his way. [0]

Cheers,

AJ

[0] [GitHub - hw-cookbooks/injector: Inject stuff into Chef](https://github.com/hw-cookbooks/injector)

On 16 April 2013 06:43, steve . [leftathome@gmail.com](mailto:leftathome@gmail.com) wrote:

> I've run defines and used LWRPs in other recipes -- seems to work, as long  
> as the defining cookbook is loaded up earlier in the run list. However, I  
> don't like the idea of leaving it up to the user to figure out whether or  
> not they need to add some\_random\_cookbook to a run list, so I've adopted the  
> pattern of "include\_recipe 'some\_random\_cookbook::default'" and simply not  
> putting anything of substance in a default recipe that isn't required across  
> the entire cookbook.
> 
> On Thu, Apr 11, 2013 at 12:07 PM, Kevin Nuckolls [kevin.nuckolls@gmail.com](mailto:kevin.nuckolls@gmail.com)  
> wrote:
> 
> > Since it's ruby, I assume it's possible to have a resource run within  
> > another scope as defined at runtime. I basically ran into a situation where  
> > I defined a variable that I used to inject information in a template in a  
> > library cookbook, then found that I also wanted access to that variable from  
> > the wrapper cookbook.
> > 
> > I could probably put it in a library, but at this point I'm just curious  
> > if anyone has done this. Can you run a resource in another cookbook's recipe  
> > if you even wanted to?
> > 
> > Not saying this is the correct way to do it, just curious if it's actually  
> > possible.
> > 
> > -Kevin

---

<div class="post-metadata">

**Author:** ![Kevin\_Nuckolls](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kevin_nuckolls/32/327_2.png) [@Kevin\_Nuckolls](https://discourse.chef.io/u/Kevin_Nuckolls)\
**Post date:** [April 16, 2013, 2:05pm UTC](https://discourse.chef.io/t/injecting-a-resource-into-another-cookbooks-recipe/3833/4 "2013-04-16T14:05:42Z")

</div>

Ah the injector is pretty cool. I'm not sure if / when I'll encounter this  
use case again, but it's a nice tool to have in the toolbox.

Thanks,  
-Kevin

On Mon, Apr 15, 2013 at 2:41 PM, AJ Christensen [aj@junglist.gen.nz](mailto:aj@junglist.gen.nz) wrote:

> A member of Heavy Water Operations (Chris Roberts, spox) just build a  
> system that allows arbitrary injection of resources at any time in the  
> recipe life cycle. Chris had noticed a pattern used like this in the  
> AWS OpsWorks tool and set out to re-implement.
> 
> Check it out. I'll endeavor to pass any feedback his way. [0]
> 
> Cheers,
> 
> AJ
> 
> [0] [GitHub - hw-cookbooks/injector: Inject stuff into Chef](https://github.com/hw-cookbooks/injector)
> 
> On 16 April 2013 06:43, steve . [leftathome@gmail.com](mailto:leftathome@gmail.com) wrote:
> 
> > I've run defines and used LWRPs in other recipes -- seems to work, as  
> > long  
> > as the defining cookbook is loaded up earlier in the run list. However,  
> > I  
> > don't like the idea of leaving it up to the user to figure out whether or  
> > not they need to add some\_random\_cookbook to a run list, so I've adopted  
> > the  
> > pattern of "include\_recipe 'some\_random\_cookbook::default'" and simply  
> > not  
> > putting anything of substance in a default recipe that isn't required  
> > across  
> > the entire cookbook.
> > 
> > On Thu, Apr 11, 2013 at 12:07 PM, Kevin Nuckolls \<  
> > [kevin.nuckolls@gmail.com](mailto:kevin.nuckolls@gmail.com)\>  
> > wrote:
> > 
> > > Since it's ruby, I assume it's possible to have a resource run within  
> > > another scope as defined at runtime. I basically ran into a situation  
> > > where  
> > > I defined a variable that I used to inject information in a template in  
> > > a  
> > > library cookbook, then found that I also wanted access to that variable  
> > > from  
> > > the wrapper cookbook.
> > > 
> > > I could probably put it in a library, but at this point I'm just curious  
> > > if anyone has done this. Can you run a resource in another cookbook's  
> > > recipe  
> > > if you even wanted to?
> > > 
> > > Not saying this is the correct way to do it, just curious if it's  
> > > actually  
> > > possible.
> > > 
> > > -Kevin
