[RESOLVED] Attribute Error


#1

Hi All,

I am getting this error when running a simple cookbook with attribute:

TypeError

no implicit conversion of String into Integer

Relevant File Content:

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

19:
20: node[“test”][“sitename”].each do |data1|
21: template “/home/ec2-user/newwwww.txt” do
22: source "template_test.erb"
23: mode "0755"
24: owner "root"
25: group "ec2-user"
26>> variables(:nam => data1[‘names’])
27: end
28: end
29:

This is attribute config:

[ec2-user@Splunk-Server recipes] cat ../attributes/default.rb default[:chef][:site][:com] = { 'port' => '8080', 'ssl' => 'yes'}; default[:chef][:site][:de] = { 'port' => '8081', 'ssl' => 'no'}; default[:chef][:site][:it] = { 'port' => '8082', 'ssl' => 'yes'}; **default[:test][:sitename] = { 'nam' => 'testattribute','value' => 'yes'}** [ec2-user@Splunk-Server recipes]

This is the recipe config:

node[“chef”][“site”].each do |sitename, data|
template “/home/ec2-user/#{sitename}.txt” do
source "template_test.erb"
mode "0755"
owner "root"
group "ec2-user"
variables(:site_name => sitename, :port => data[‘port’], :ssl => data[‘ssl’])
end
end

node[“test”][“sitename”].each do |data1|
template “/home/ec2-user/newwwww.txt” do
source "template_test.erb"
mode "0755"
owner "root"
group "ec2-user"
variables(:nam => data1[‘names’])
end
end

I am not able to find out what is the error in my config. Any help would be appreciated!

Krish


#2

The problem here is that you’re trying to iterate over the hash node[:test][:sitename] but it doesn’t look the same as the earlier Hash you used from the examples.

# I've reformatted this to be consistent
default['chef']['site]['com'] = { 'port' => '8080', 'ssl' => 'yes' }
default['chef']['site]['de'] = { 'port' => '8081', 'ssl' => 'no' }
default['chef']['site]['it'] = { 'port' => '8082', 'ssl' => 'yes' }

As explained earlier, attributes are hashes so the when you iterate over node['chef']['site'] there are three sub-hashes com, de, it to iterate over. For each of those we get they key (sitename) and then the value (data). The data is itself a Hash and so we can call data['port'] and data['ssl'] to get the values out.

With that explained the problem with the second part is that node["test"]["sitename"] isn’t formatted as you expect, you probably want something that looks more like

# attributes
default['test']['sitename'] = { 'nam' => 'testattribute','value' => 'yes'} 
node["test"].each do |key, value|
  template "/home/ec2-user/newwwww.txt" do
    source "template_test.erb"
    mode "0755"
    owner "root"
    group "ec2-user"
    variables(:nam => value['nam'])
  end
end

In the above example we aren’t using the key value (sitename) at all but you could depending on your needs. Hopefully illustrating why the first works and the second does not is helpful.

It would probably be best to join us in the Slack channel or in IRC (freenode #chef) so that folks can help you with these in a bit more interactive medium.


#3

Hurray! It worked and it make more sense now!