# How to interchange data between different recipes on open source chef

**URL:** https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425
**Category:** Chef Infra (archive)
**Created:** [September 10, 2013, 3:12am UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425 "2013-09-10T03:12:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sam\_Su](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Sam\_Su](https://discourse.chef.io/u/Sam_Su)
#### Post date: [September 10, 2013, 3:12am UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425/1 "2013-09-10T03:12:47Z")

</div>

Hi,

I have two recipes my open source chef server and need to interchange some  
data between them. I wondering how to do that. If someone give a hint, it  
will much appreciated.

PS: I tried to write the data into a databag, but failed as below. It looks  
like recipes save data into a databag is allowed.

## \*Net::HTTPServerException

405 “Method Not Allowed”

## Cookbook Trace:

/var/chef/cache/cookbooks/mysql/recipes/server.rb:34:in `from\_file’

## Relevant File Content:

/var/chef/cache/cookbooks/mysql/recipes/server.rb:

30:  
31: #if Chef::DataBag.list.key?(‘unique\_name’)  
32: new\_databag = Chef::DataBag.new  
33: new\_databag.name(‘unique\_name’)  
34\>\> new\_databag.save  
35: #end  
36: \*

Thanks,  
Sam

---

<div class="post-metadata">

### Author: ![David\_Petzel](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/david_petzel/32/38_2.png) [@David\_Petzel](https://discourse.chef.io/u/David_Petzel)
#### Post date: [September 10, 2013, 3:37am UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425/2 "2013-09-10T03:37:24Z")

</div>

I have not done it myself, but I did think editing databags from a recipe  
was possible, and is documented here:

> **[About Data Bags](https://docs.chef.io/data_bags/)**
>
> Data bags store global variables as JSON data. Data bags are indexed for searching and can be loaded by a cookbook or accessed during a search.

If you don't need the data to persist across chef runs (IE you just need to  
exchange it during the course of a single run) I have used the `node. run_state` variable.  
so something like:  
recipe1.rb:  
node.run\_state[:my\_data] = "Hello"  
recipe2.rb:  
puts node.run\_state[:my\_data]

I have two recipes my open source chef server and need to interchange some

> data between them. I wondering how to do that. If someone give a hint, it  
> will much appreciated.
> 
> PS: I tried to write the data into a databag, but failed as below. It  
> looks like recipes save data into a databag is allowed.
> 
> ## \*Net::HTTPServerException
> 
> 405 "Method Not Allowed"
> 
> -

---

<div class="post-metadata">

### Author: ![John\_Dewey](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/john_dewey/32/457_2.png) [@John\_Dewey](https://discourse.chef.io/u/John_Dewey)
#### Post date: [September 10, 2013, 4:25am UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425/3 "2013-09-10T04:25:09Z")

</div>

++

I use this method in the reboot-handler cookbook ([cookbook-reboot-handler/files/default/reboot.rb at master · retr0h/cookbook-reboot-handler · GitHub](https://github.com/retr0h/cookbook-reboot-handler/blob/master/files/default/reboot.rb))

John

On Monday, September 9, 2013 at 8:37 PM, David Petzel wrote:

> I have not done it myself, but I did think editing databags from a recipe was possible, and is documented here: [About Data Bags](http://docs.opscode.com/essentials_data_bags.html#create-and-edit)
> 
> If you don't need the data to persist across chef runs (IE you just need to exchange it during the course of a single run) I have used the `node.run_state` variable.  
> so something like:  
> recipe1.rb:  
> node.run\_state[:my\_data] = "Hello"  
> recipe2.rb:  
> puts node.run\_state[:my\_data]
> 
> > I have two recipes my open source chef server and need to interchange some data between them. I wondering how to do that. If someone give a hint, it will much appreciated.
> > 
> > PS: I tried to write the data into a databag, but failed as below. It looks like recipes save data into a databag is allowed.
> > 
> > ## Net::HTTPServerException
> > 
> > 405 "Method Not Allowed"

---

<div class="post-metadata">

### Author: ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)
#### Post date: [September 10, 2013, 2:58pm UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425/4 "2013-09-10T14:58:59Z")

</div>

On Monday, September 9, 2013 at 8:12 PM, Sam Su wrote:

> Hi,
> 
> I have two recipes my open source chef server and need to interchange some data between them. I wondering how to do that. If someone give a hint, it will much appreciated.
> 
> PS: I tried to write the data into a databag, but failed as below. It looks like recipes save data into a databag is allowed.
> 
> ## Net::HTTPServerException
> 
> 405 "Method Not Allowed"
> 
> ## Cookbook Trace:
> 
> /var/chef/cache/cookbooks/mysql/recipes/server.rb:34:in `from\_file'
> 
> ## Relevant File Content:
> 
> /var/chef/cache/cookbooks/mysql/recipes/server.rb:
> 
> 30:  
> 31: #if Chef::DataBag.list.key?('unique\_name')  
> 32: new\_databag = Chef::DataBag.new  
> 33: new\_databag.name (http://new\_databag.name)('unique\_name')  
> 34\>\> new\_databag.save  
> 35: #end  
> 36:

Data bags can be a little awkward. A data bag is just a container into which you can put data bag items. Since the data bag part doesn't have any data, there's nothing to update, which is what you're trying to do here. For what you're trying to do, data bag items would work.

That said, for passing in-memory data between recipes within a chef run, you're better off setting node attributes or using the node.run\_state hash.

> Thanks,  
> Sam

HTH,

--  
Daniel DeLeo

---

<div class="post-metadata">

### Author: ![Seth\_Falcon\_01](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/seth_falcon_01/32/429_2.png) [@Seth\_Falcon\_01](https://discourse.chef.io/u/Seth_Falcon_01)
#### Post date: [September 10, 2013, 3:35pm UTC](https://discourse.chef.io/t/how-to-interchange-data-between-different-recipes-on-open-source-chef/4425/5 "2013-09-10T15:35:03Z")

</div>

Hi Sam,

[sam.debian@gmail.com](mailto:sam.debian@gmail.com) writes:

> I have two recipes my open source chef server and need to interchange some  
> data between them. I wondering how to do that. If someone give a hint, it  
> will much appreciated.
> 
> PS: I tried to write the data into a databag, but failed as below. It looks  
> like recipes save data into a databag is allowed.

> ## \*Net::HTTPServerException
> 
> 405 "Method Not Allowed"

I think the error you are seeing is this bug:

[https://tickets.opscode.com/browse/CHEF-4435](https://tickets.opscode.com/browse/CHEF-4435)

You might try creating the data bag ahead of time since actual data  
lives in a data bag item in a given data bag.

- seth

--  
Seth Falcon | Development Lead | Opscode | @sfalcon
