Nested data bag data to template file returns “can't convert String into Integer"

Hello,

I’m creating simple test recipe with a template and data bag. What I’m trying
to do is creating a config file from data bag that has simple nested
information, but I receive error “can’t convert String into Integer” and I
can’t find how to get data bag item “part.name

Here are my setting files.

  1. recipe/default.rb
    data1 = data_bag_item( ‘mytest’, ‘qa’ )[‘test’]
    data2 = data_bag_item( ‘mytest’, ‘qa’ )
    template “/opt/env/test.cfg” do
    source "test.erb"
    action :create_if_missing
    mode 0664
    owner "root"
    group "root"
    variables({
    :pepe1 => data1[‘part.name’],
    :pepe2 => data2[‘transport.tcp.ip2’]
    })
    end

2)my data bag named "mytest"
test.json
{
“test”:[
{
part.name”: “L12”,
“transport.tcp.ip”: “111.111.111.111”,
“transport.tcp.port”: “9199”
}
],
“transport.tcp.ip2”: “222.222.222.222”,
“id”: “qa”
}

$knife data bag show mytest qa
id: qa
test:
part.name: L12
transport.tcp.ip: 111.111.111.111
transport.tcp.port: 9199
transport.tcp.ip2: 222.222.222.222

3)template file test.erb

part.name=<%= @pepe1 %>
transport.tcp.binding=<%= @pepe2 %>

Error returns when I run chef-client on my server,

[2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
[]’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
from_file’ [2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile
error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in[]’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
block in
from_file’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:infrom_file’
[2013-06-24T19:50:38+00:00] DEBUG: backtrace entry for compile error:
’/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in `[]’’
[2013-06-24T19:50:38+00:00] DEBUG: Line number of compile error: ‘19’

Recipe Compile Error in
/var/chef/cache/cookbooks/config_test/recipes/default.rb

TypeError

can’t convert String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in []’
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file’ /var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
`from_file’

Relevant File Content:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:

12: template “/opt/env/test.cfg” do
13: source "test.erb"
14: action :create_if_missing
15: mode 0664
16: owner "root"
17: group "root"
18: variables({
19>> :pepe1 => data1[‘part.name’],
20: :pepe2 => data2[‘transport.tcp.ip2’]
21: })
22: end 23:

I tried many things and if I comment out “pepe1 => data1[‘part.name’],”, then
:pepe2 => data2[‘transport.tcp.ip2’] works fine. only nested data "part.name"
cannot be set to @pepe1.

Does anyone knows why I receive the errors?

thanks,

On Tuesday, June 25, 2013 at 8:10 AM, dpark@smart-trade.net wrote:

Hello,

I'm creating simple test recipe with a template and data bag. What I'm trying
to do is creating a config file from data bag that has simple nested
information, but I receive error "can't convert String into Integer" and I
can't find how to get data bag item "part.name (http://part.name)"

Here are my setting files.

  1. recipe/default.rb
    data1 = data_bag_item( 'mytest', 'qa' )['test']
    data2 = data_bag_item( 'mytest', 'qa' )
    template "/opt/env/test.cfg" do
    source "test.erb"
    action :create_if_missing
    mode 0664
    owner "root"
    group "root"
    variables({
    :pepe1 => data1['part.name (http://part.name)'],
    :pepe2 => data2['transport.tcp.ip2']
    })
    end

2)my data bag named "mytest"
test.json
{
"test":[
{
"part.name (http://part.name)": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
}
],
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

$knife data bag show mytest qa
id: qa
test:
part.name (http://part.name): L12
transport.tcp.ip: 111.111.111.111
transport.tcp.port: 9199
transport.tcp.ip2: 222.222.222.222

3)template file test.erb

part.name (http://part.name)=<%= @pepe1 %>
transport.tcp.binding=<%= @pepe2 %>

Error returns when I run chef-client on my server,

[2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
',/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
from_file' [2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile
error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in',/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
block in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:infrom_file'
[2013-06-24T19:50:38+00:00] DEBUG: backtrace entry for compile error:
'/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in `''
[2013-06-24T19:50:38+00:00] DEBUG: Line number of compile error: '19'

Recipe Compile Error in
/var/chef/cache/cookbooks/config_test/recipes/default.rb

TypeError

can't convert String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in '
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file' /var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
`from_file'

Relevant File Content:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:

12: template "/opt/env/test.cfg" do
13: source "test.erb"
14: action :create_if_missing
15: mode 0664
16: owner "root"
17: group "root"
18: variables({
19>> :pepe1 => data1['part.name (http://part.name)'],
20: :pepe2 => data2['transport.tcp.ip2']
21: })
22: end 23:

I tried many things and if I comment out "pepe1 => data1['part.name (http://part.name)'],", then
:pepe2 => data2['transport.tcp.ip2'] works fine. only nested data "part.name (http://part.name)"
cannot be set to @pepe1.

Does anyone knows why I receive the errors?

thanks,
That type error is saying that you're accessing an Array as if it was a hash. Try pretty printing your data1 variable like

require 'pp'
pp data1

That should show you the data structure. BTW, you can also use different formats with knife, e.g. knife TYPE show NAME -Fpp (for ruby pretty print) or -Fj (for json). The default format is optimized for readability at the expense of obscuring some details about the data.

--
Daniel DeLeo

Hi Daniel,
Thanks for the reply.

I followed as you mentioned and used “require” and “pp” in my recipe and it shows like below.
For me, it look like hash. I don’t know how to put value of “part.name” to variable.

[{“part.name”=>“L12”,
“transport.tcp.ip”=>“111.111.111.111”,
“transport.tcp.port”=>“9199”}]

Also used Fpp and Fj option.

knife data bag show mytest qa -Fpp

{“test”=>
[{“part.name”=>“L12”,
“transport.tcp.ip”=>“111.111.111.111”,
“transport.tcp.port”=>“9199”}],
“transport.tcp.ip2”=>“222.222.222.222”,
“id”=>“qa”}

knife data bag show mytest qa -Fj

{
“test”: [
{
part.name”: “L12”,
“transport.tcp.ip”: “111.111.111.111”,
“transport.tcp.port”: “9199”
}
],
“transport.tcp.ip2”: “222.222.222.222”,
“id”: “qa”
}

From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel DeLeo
Sent: Tuesday, June 25, 2013 11:33 AM
To: chef@lists.opscode.com
Subject: [chef] Re: nested data bag data to template file returns “can’t convert String into Integer"

On Tuesday, June 25, 2013 at 8:10 AM, dpark@smart-trade.netmailto:dpark@smart-trade.net wrote:
Hello,

I’m creating simple test recipe with a template and data bag. What I’m trying
to do is creating a config file from data bag that has simple nested
information, but I receive error “can’t convert String into Integer” and I
can’t find how to get data bag item “part.namehttp://part.name

Here are my setting files.

  1. recipe/default.rb
    data1 = data_bag_item( ‘mytest’, ‘qa’ )[‘test’]
    data2 = data_bag_item( ‘mytest’, ‘qa’ )
    template “/opt/env/test.cfg” do
    source "test.erb"
    action :create_if_missing
    mode 0664
    owner "root"
    group "root"
    variables({
    :pepe1 => data1[‘part.namehttp://part.name’],
    :pepe2 => data2[‘transport.tcp.ip2’]
    })
    end

2)my data bag named "mytest"
test.json
{
“test”:[
{
part.namehttp://part.name”: “L12”,
“transport.tcp.ip”: “111.111.111.111”,
“transport.tcp.port”: “9199”
}
],
“transport.tcp.ip2”: “222.222.222.222”,
“id”: “qa”
}

$knife data bag show mytest qa
id: qa
test:
part.namehttp://part.name: L12
transport.tcp.ip: 111.111.111.111
transport.tcp.port: 9199
transport.tcp.ip2: 222.222.222.222

3)template file test.erb

part.namehttp://part.name=<%= @pepe1 %>
transport.tcp.binding=<%= @pepe2 %>

Error returns when I run chef-client on my server,

[2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
[]’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
from_file’ [2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile
error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in[]’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
block in
from_file’,/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:infrom_file’
[2013-06-24T19:50:38+00:00] DEBUG: backtrace entry for compile error:
’/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in `[]’’
[2013-06-24T19:50:38+00:00] DEBUG: Line number of compile error: ‘19’

Recipe Compile Error in
/var/chef/cache/cookbooks/config_test/recipes/default.rb

TypeError

can’t convert String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in []’
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file’ /var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
`from_file’

Relevant File Content:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:

12: template “/opt/env/test.cfg” do
13: source "test.erb"
14: action :create_if_missing
15: mode 0664
16: owner "root"
17: group "root"
18: variables({
19>> :pepe1 => data1[‘part.namehttp://part.name’],
20: :pepe2 => data2[‘transport.tcp.ip2’]
21: })
22: end 23:

I tried many things and if I comment out “pepe1 => data1[‘part.namehttp://part.name’],”, then
:pepe2 => data2[‘transport.tcp.ip2’] works fine. only nested data "part.namehttp://part.name"
cannot be set to @pepe1.

Does anyone knows why I receive the errors?

thanks,
That type error is saying that you’re accessing an Array as if it was a hash. Try pretty printing your data1 variable like

require 'pp'
pp data1

That should show you the data structure. BTW, you can also use different formats with knife, e.g. knife TYPE show NAME -Fpp (for ruby pretty print) or -Fj (for json). The default format is optimized for readability at the expense of obscuring some details about the data.


Daniel DeLeo

Hi,

you should remove the square brackets from your data bag. It should look like this:

{
"test": {
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
},
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

alternatively, you need to access the part.name by first accessing the array's first element:

data1 = data_bag_item( 'mytest', 'qa' )['test'].first
or, similar
data1 = data_bag_item( 'mytest', 'qa' )['test'][0]

If you want to nicely nest your data bag, this could be what you initially tried to do? But I'm guessing:

{
"test": {
"part": {
"name": "L12",
"transport": {
"tcp": {
"ip": "111.111.111.111",
"port": "9199"
}
}
}
},
"transport": {
"tcp": {
"ip2": "222.222.222.222"
}
},
"id": "qa"
}

hope this helps,
Matthias

Am 25.06.2013 um 19:16 schrieb Dalho PARK dpark@smart-trade.net:

Hi Daniel,
Thanks for the reply.

I followed as you mentioned and used “require” and “pp” in my recipe and it shows like below.
For me, it look like hash. I don’t know how to put value of “part.name” to variable.

[{"part.name"=>"L12",
"transport.tcp.ip"=>"111.111.111.111",
"transport.tcp.port"=>"9199"}]

Also used Fpp and Fj option.

knife data bag show mytest qa -Fpp

{"test"=>
[{"part.name"=>"L12",
"transport.tcp.ip"=>"111.111.111.111",
"transport.tcp.port"=>"9199"}],
"transport.tcp.ip2"=>"222.222.222.222",
"id"=>"qa"}

knife data bag show mytest qa -Fj

{
"test": [
{
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
}
],
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel DeLeo
Sent: Tuesday, June 25, 2013 11:33 AM
To: chef@lists.opscode.com
Subject: [chef] Re: nested data bag data to template file returns “can't convert String into Integer"

On Tuesday, June 25, 2013 at 8:10 AM, dpark@smart-trade.net wrote:

Hello,

I'm creating simple test recipe with a template and data bag. What I'm trying
to do is creating a config file from data bag that has simple nested
information, but I receive error "can't convert String into Integer" and I
can't find how to get data bag item "part.name"

Here are my setting files.

  1. recipe/default.rb
    data1 = data_bag_item( 'mytest', 'qa' )['test']
    data2 = data_bag_item( 'mytest', 'qa' )
    template "/opt/env/test.cfg" do
    source "test.erb"
    action :create_if_missing
    mode 0664
    owner "root"
    group "root"
    variables({
    :pepe1 => data1['part.name'],
    :pepe2 => data2['transport.tcp.ip2']
    })
    end

2)my data bag named "mytest"
test.json
{
"test":[
{
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
}
],
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

$knife data bag show mytest qa
id: qa
test:
part.name: L12
transport.tcp.ip: 111.111.111.111
transport.tcp.port: 9199
transport.tcp.ip2: 222.222.222.222

3)template file test.erb

part.name=<%= @pepe1 %>
transport.tcp.binding=<%= @pepe2 %>

Error returns when I run chef-client on my server,

[2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
',/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
from_file' [2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile
error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in',/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
block in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:infrom_file'
[2013-06-24T19:50:38+00:00] DEBUG: backtrace entry for compile error:
'/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in `''
[2013-06-24T19:50:38+00:00] DEBUG: Line number of compile error: '19'

Recipe Compile Error in
/var/chef/cache/cookbooks/config_test/recipes/default.rb

TypeError

can't convert String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in '
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file' /var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
`from_file'

Relevant File Content:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:

12: template "/opt/env/test.cfg" do
13: source "test.erb"
14: action :create_if_missing
15: mode 0664
16: owner "root"
17: group "root"
18: variables({
19>> :pepe1 => data1['part.name'],
20: :pepe2 => data2['transport.tcp.ip2']
21: })
22: end 23:

I tried many things and if I comment out "pepe1 => data1['part.name'],", then
:pepe2 => data2['transport.tcp.ip2'] works fine. only nested data "part.name"
cannot be set to @pepe1.

Does anyone knows why I receive the errors?

thanks,
That type error is saying that you're accessing an Array as if it was a hash. Try pretty printing your data1 variable like

require 'pp'
pp data1

That should show you the data structure. BTW, you can also use different formats with knife, e.g. knife TYPE show NAME -Fpp (for ruby pretty print) or -Fj (for json). The default format is optimized for readability at the expense of obscuring some details about the data.

--
Daniel DeLeo

Hi Matthias,

After removing the square blacked, everything went well.

Thank you so much.

Go
-----Original Message-----
From: Matthias Marschall [mailto:matthias.marschall@gmail.com] On Behalf Of Matthias Marschall
Sent: Tuesday, June 25, 2013 1:42 PM
To: chef@lists.opscode.com
Subject: [chef] Re: RE: Re: nested data bag data to template file returns “can't convert String into Integer"

Hi,

you should remove the square brackets from your data bag. It should look like this:

{
"test": {
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
},
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

alternatively, you need to access the part.name by first accessing the array's first element:

data1 = data_bag_item( 'mytest', 'qa' )['test'].first
or, similar
data1 = data_bag_item( 'mytest', 'qa' )['test'][0]

If you want to nicely nest your data bag, this could be what you initially tried to do? But I'm guessing:

{
"test": {
"part": {
"name": "L12",
"transport": {
"tcp": {
"ip": "111.111.111.111",
"port": "9199"
}
}
}
},
"transport": {
"tcp": {
"ip2": "222.222.222.222"
}
},
"id": "qa"
}

hope this helps,
Matthias

Am 25.06.2013 um 19:16 schrieb Dalho PARK dpark@smart-trade.net:

Hi Daniel,
Thanks for the reply.

I followed as you mentioned and used “require” and “pp” in my recipe and it shows like below.
For me, it look like hash. I don’t know how to put value of “part.name” to variable.

[{"part.name"=>"L12",
"transport.tcp.ip"=>"111.111.111.111",
"transport.tcp.port"=>"9199"}]

Also used Fpp and Fj option.

knife data bag show mytest qa -Fpp

{"test"=>
[{"part.name"=>"L12",
"transport.tcp.ip"=>"111.111.111.111",
"transport.tcp.port"=>"9199"}],
"transport.tcp.ip2"=>"222.222.222.222",
"id"=>"qa"}

knife data bag show mytest qa -Fj

{
"test": [
{
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
}
],
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel
DeLeo
Sent: Tuesday, June 25, 2013 11:33 AM
To: chef@lists.opscode.com
Subject: [chef] Re: nested data bag data to template file returns “can't convert String into Integer"

On Tuesday, June 25, 2013 at 8:10 AM, dpark@smart-trade.net wrote:

Hello,

I'm creating simple test recipe with a template and data bag. What I'm
trying to do is creating a config file from data bag that has simple
nested information, but I receive error "can't convert String into
Integer" and I can't find how to get data bag item "part.name"

Here are my setting files.

  1. recipe/default.rb
    data1 = data_bag_item( 'mytest', 'qa' )['test']
    data2 = data_bag_item( 'mytest', 'qa' ) template "/opt/env/test.cfg"
    do source "test.erb"
    action :create_if_missing
    mode 0664
    owner "root"
    group "root"
    variables({
    :pepe1 => data1['part.name'],
    :pepe2 => data2['transport.tcp.ip2']
    })
    end

2)my data bag named "mytest"
test.json
{
"test":[
{
"part.name": "L12",
"transport.tcp.ip": "111.111.111.111",
"transport.tcp.port": "9199"
}
],
"transport.tcp.ip2": "222.222.222.222",
"id": "qa"
}

$knife data bag show mytest qa
id: qa
test:
part.name: L12
transport.tcp.ip: 111.111.111.111
transport.tcp.port: 9199
transport.tcp.ip2: 222.222.222.222

3)template file test.erb

part.name=<%= @pepe1 %>
transport.tcp.binding=<%= @pepe2 %>

Error returns when I run chef-client on my server,

[2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace of compile error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
',/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inbloc
k in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12
:in from_file' [2013-06-24T19:50:38+00:00] DEBUG: filtered backtrace
of compile
error:
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in',/var
/chef/cache/cookbooks/config_test/recipes/default.rb:19:in
block in
from_file',/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:infrom_file'
[2013-06-24T19:50:38+00:00] DEBUG: backtrace entry for compile error:
'/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in `''
[2013-06-24T19:50:38+00:00] DEBUG: Line number of compile error: '19'

Recipe Compile Error in
/var/chef/cache/cookbooks/config_test/recipes/default.rb

TypeError

can't convert String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:in '
/var/chef/cache/cookbooks/config_test/recipes/default.rb:19:inblock in
from_file'
/var/chef/cache/cookbooks/config_test/recipes/default.rb:12:in
`from_file'

Relevant File Content:

/var/chef/cache/cookbooks/config_test/recipes/default.rb:

12: template "/opt/env/test.cfg" do
13: source "test.erb"
14: action :create_if_missing
15: mode 0664
16: owner "root"
17: group "root"
18: variables({
19>> :pepe1 => data1['part.name'],
20: :pepe2 => data2['transport.tcp.ip2']
21: })
22: end 23:

I tried many things and if I comment out "pepe1 =>
data1['part.name'],", then
:pepe2 => data2['transport.tcp.ip2'] works fine. only nested data "part.name"
cannot be set to @pepe1.

Does anyone knows why I receive the errors?

thanks,
That type error is saying that you're accessing an Array as if it was
a hash. Try pretty printing your data1 variable like

require 'pp'
pp data1

That should show you the data structure. BTW, you can also use different formats with knife, e.g. knife TYPE show NAME -Fpp (for ruby pretty print) or -Fj (for json). The default format is optimized for readability at the expense of obscuring some details about the data.

--
Daniel DeLeo