Sharing Variables across Recipies


#1

I have situation in my cookbook where i extract some values form data_bags and put them onto local variables on recipes after some manipulation of them. I am running on to a situation where i need to repeat this logic across many cookbooks as i see that when i do it in “default.rb” and then use them in include_recipe’s I see chef complaining that the variable is undefined, am not sure why so ideally when if I understand correctly all the recipes are compiled on to a single class by chef during converge so conceptually they should be visible.

Is there a way how i can share the variables declared in a recipe?

-Kamesh


#2

instance variables are shared across recipes, as each of the recipe is a
separate object. you can use node.run_state as small cross recipe storage
place. careful with this approach though, it might break why_run mode,
you can also use a dedicated module which exposes some common storage
place, as well as wraps reusable code, i prefer that, as its clean and i
can change the shared storage logic transparently,
regards
ranjib

On Sat, Oct 25, 2014 at 4:29 AM, Kamesh Sampath kamesh.sampath@hotmail.com
wrote:

I have situation in my cookbook where i extract some values form data_bags
and put them onto local variables on recipes after some manipulation of
them. I am running on to a situation where i need to repeat this logic
across many cookbooks as i see that when i do it in “default.rb” and then
use them in include_recipe’s I see chef complaining that the variable is
undefined, am not sure why so ideally when if I understand correctly all
the recipes are compiled on to a single class by chef during converge so
conceptually they should be visible.

Is there a way how i can share the variables declared in a recipe?

-Kamesh


#3

Normally that what node attribute are for, otherwise if you want something static you can use databags.

If you want more specific answer I think you should provide an example of what you are looking for.

-----Original Message-----
From: Kamesh Sampath [mailto:kamesh.sampath@hotmail.com]
Sent: samedi 25 octobre 2014 13:30
To: chef@lists.opscode.com
Cc: charles@getchef.com
Subject: [chef] Sharing Variables across Recipies

I have situation in my cookbook where i extract some values form data_bags and put them onto local variables on recipes after some manipulation of them. I am running on to a situation where i need to repeat this logic across many cookbooks as i see that when i do it in “default.rb” and then use them in include_recipe’s I see chef complaining that the variable is undefined, am not sure why so ideally when if I understand correctly all the recipes are compiled on to a single class by chef during converge so conceptually they should be visible.

Is there a way how i can share the variables declared in a recipe?

-Kamesh