# Sharing Variables across Recipies

**URL:** <https://discourse.chef.io/t/sharing-variables-across-recipies/5850>\
**Category:** Chef Infra (archive)\
**Created:** [October 25, 2014, 11:29am UTC](https://discourse.chef.io/t/sharing-variables-across-recipies/5850 "2014-10-25T11:29:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamesh\_Sampath](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kamesh_sampath/32/221_2.png) [@Kamesh\_Sampath](https://discourse.chef.io/u/Kamesh_Sampath)\
**Post date:** [October 25, 2014, 11:29am UTC](https://discourse.chef.io/t/sharing-variables-across-recipies/5850/1 "2014-10-25T11:29:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ranjib](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ranjib/32/1040_2.png) [@Ranjib](https://discourse.chef.io/u/Ranjib)\
**Post date:** [October 25, 2014, 5:18pm UTC](https://discourse.chef.io/t/sharing-variables-across-recipies/5850/2 "2014-10-25T17:18:19Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![Jeremy\_Mauro](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jeremy\_Mauro](https://discourse.chef.io/u/Jeremy_Mauro)\
**Post date:** [October 27, 2014, 9:05am UTC](https://discourse.chef.io/t/sharing-variables-across-recipies/5850/3 "2014-10-27T09:05:09Z")

</div>

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](mailto:kamesh.sampath@hotmail.com)]  
Sent: samedi 25 octobre 2014 13:30  
To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
Cc: [charles@getchef.com](mailto: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
