Search code examples
chef-infrachef-recipecookbookrecipelwrp

using include_recipe in LWRP and not run_context.include_recipe


I started using include_recipe in my LWRPs and it just worked- didn't know about run_context.include_recipe. I'm not sure I understand the difference and read some stuff on the internets makes it sound like regular include_recipe shouldn't even work in a LWRP?

It seems to work fine, but just curious if I'm doing something wrong here. I don't want include_recipe to run recipes each time the resource is called I'm just using it like a regular include- resources that depend on certain recipes to setup stuff up first just include them.

I assumed that the included recipes run only the first time the resource is called and if the resource is called multiple times the included_recipe is not re-run.


Solution

  • The problem with include_recipe inside an LWRP is that that the resources from the included recipe end up in the execution context for the provider's action which is basically like a sub-context. Unfortunately a good solution is hard to come by. As with your last question, I do have a solution in my Poise library but it's very intricate and all the "simple" solutions have their own downsides. If you haven't read it, check out https://coderanger.net/two-pass/, that goes over the internal structure of how the DSL works. Add to that post that the inside of a provider is basically its own version of the compile-and-converge cycle with and its own resource collection. You'll have to imagine how the execution sequencing works and sort out which resources go in which collections and where the converge pointers are (basically like the instruction pointer and subroutines work in "normal code").