# Passing JSON into cookbook

**URL:** <https://discourse.chef.io/t/passing-json-into-cookbook/7122>\
**Category:** Chef Infra (archive)\
**Created:** [October 23, 2015, 2:31pm UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122 "2015-10-23T14:31:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@Simon\_Hawkins](https://discourse.chef.io/u/Simon_Hawkins)\
**Post date:** [October 23, 2015, 2:31pm UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/1 "2015-10-23T14:31:47Z")

</div>

Hi There,

In my ./attributes directory in the default.rb I have the following:  
default[:db][:jdbc\_sqlserver\_url] = “test”

And in ./templates/default in the template.rb file I have:  
url \<%= node[:db][:jdbc\_sqlserver\_url] %\>

And want to use in the default recipe:

template “/path/to/config.conf” do  
source "default.erb"  
end

And then pass in:

{  
“db”: {  
“jdbc\_sqlserver\_url”: “bob”  
}  
}

And write it to the config.conf file.

How do i pass the variables in on the “template “/path/to/config.conf” do” definition?

Cheers,  
Simon.

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [October 23, 2015, 2:57pm UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/2 "2015-10-23T14:57:58Z")

</div>

What the problem ? it’s really unclear to me what you want to do there.

A guess if I understood properly is that you want to change the default value set in your cookbook.

First option:

The template could become:

```
url <%= @jdbc_sqlserver_url %>

```

And the template call be:

```
template "/path/to/config.conf" do
  source "default.erb"
  variables ("jdbc_sqlserver_url" => node['db']['jdbc_sqlserver_url']) 
end

```

And you can change the variable value for another template using the same default.erb file

Second option:  
If you wish to override the url value for a specific node, create a wrapper cookbook which:

1. `depends "original"`in its metadata.rb file
2. redefine the attribute in its attribute file
3. call `include_recipe 'original_cookbook'` in its recipe file

and set this wrapper in your node runlist instead of original one.

---

<div class="post-metadata">

**Author:** ![M\_G](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@M\_G](https://discourse.chef.io/u/M_G)\
**Post date:** [October 24, 2015, 12:51am UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/3 "2015-10-24T00:51:14Z")

</div>

Hi Simon  
template config.conf do source “my.conf.erb” mode “0644” ---- whatever read,write execute permissions you want on the file variables( :var1 =\> node[:attr1][:childattr1], :var2 =\> node[:attr1][:childattr2] )end  
Assume your my.conf.erb file is as follows:  
key1=\<%= @var1 %\>key2=\<%= @var2 %\>

HTH

---

<div class="post-metadata">

**Author:** ![Simon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@Simon\_Hawkins](https://discourse.chef.io/u/Simon_Hawkins)\
**Post date:** [October 26, 2015, 2:34pm UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/4 "2015-10-26T14:34:14Z")

</div>

Many thanks all, it works!

Within the config file I have:  
(they have a comment sign in front, #)

#set.myvalue.initialSize=50  
#set.mymin.minval=10  
#set.mymax.maxvalue=40  
#set.maxnumber=43  
#set.minmumber=24

Is there a way of using an if jsonvalue = enabled, uncomment the items above. I know they are defined in the template file as variables so I cant just uncomment the “key” unless there’s a way of doing this?

Cheers,  
Simon.

---

<div class="post-metadata">

**Author:** ![Simon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@Simon\_Hawkins](https://discourse.chef.io/u/Simon_Hawkins)\
**Post date:** [October 26, 2015, 3:14pm UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/5 "2015-10-26T15:14:02Z")

</div>

Unless i could define if to use mytemplate1.erb or mytemplate2.erb depending on the attribute?

---

<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 27, 2015, 4:46am UTC](https://discourse.chef.io/t/passing-json-into-cookbook/7122/6 "2015-10-27T04:46:06Z")

</div>

yeah, you can define the source parameter based on attributes.

```ruby
if node.foo.bar
 template_source = 'x.erb'
else
 template_source = 'y.erb'
end

template '/path/to/file' do
 source template_source
end

```

also, you can do conditionals straight inside erb templates

```auto
<% if node.foo.enable %>
<% end%>

```

lastly, you can also use template partials. read chef docs and erb , that should answer a lot of things
