Reputation: 4386
I have the following node definition:
{
"prestashop_sites" : {
"site1": { "username": "dave", "password" :"password123", "URL":"www.madeup.com" },
"site2": { "username": "dave2", "password" :"password12", "URL":"www.madeup2.com" }
},
"run_list": [
"recipe[prestashop]"
]
}
And the recipe:
node["prestashop_sites"].each do |site|
username site['username']
Chef::Log.debug("Found a server: #{site['username']}")
end
remote_file "/tmp/prestashop152.zip" do
source "http://www.prestashop.com/download/old/prestashop_1.5.2.0.zip"
mode "0644"
checksum "37aee9ef5388376e4377aeb240ab027e"
backup false
not_if "test -f /tmp/prestashop152.zip"
end
execute "unzip -o /tmp/prestashop152.zip -d #{node[:prestashop][:location]}" do
not_if "test -f /var/www/#{node[:prestashop][:user]}/prestashop/index.php"
end
So my goal is to install several instances of prestashop (after I complete the script).
But i'm stuck:
10: node["prestashop_sites"].each do |site|
11>> Chef::Log.debug("Found a server: #{site['username']}")
12: end
Mon, 12 Nov 2012 21:26:14 +0100] DEBUG: Re-raising exception:
TypeError - can't convert String into Integer
Any idea why ?!
Upvotes: 3
Views: 2874
Reputation: 21206
You have a hash as a subelement of *node["prestashop_sites"]*, not an array. So you have to provide 2 variables to each method. 1 is for key, another for value:
10>>! node["prestashop_sites"].each do |key, site|
11: Chef::Log.debug("Found a server: name: #{key}, #{site['username']}")
12: end
Will print "Found a server: name: site1, dave" for the first site.
The "strange" error you get: TypeError - can't convert String into Integer, is because when you provide only 1 variable to the each method, ruby tries to put both key and value into that variable. Which ends the value of [key, value] assigned, which in your case means that
site == ["site", { "username": "dave", "password" :"password123", "URL":"www.madeup.com" }]
An array actually, and you try to access an array element with string index and not integer.
Upvotes: 5