How to load a json object in a ruby block and pass to a template


#1

Hi

In a ruby block I am trying to load a json object and passing the hash to a
template. The json file exists. How to I load a json object do I cna
pass to a

node[:neoservers] should look lie this [xxx.xxx.xxx.xxx,yyy.yyy.yyy.yyy]

ruby_block “stuff” do
block do
contents = File.read(’/tmp/cluster_ipaddress’)
existing_nodes = JSON.parse(contents)
require 'set’
neoservers = [node[:ipaddress]] + existing_nodes
neoservers = neoservers.to_set.to_a.sort
node.default[“neoservers”]=neoservers
end
action :run
end

template “/home/ubuntu/neo4j/conf/neo4j.properties” do
path "/home/ubuntu/neo4j/conf/neo4j.properties"
source "neo4j.properties.erb"
owner "root"
group "root"
mode "0644"
variables({
:server_id => server_id, :neoservers => node[:neoservers]
})
#notifies :restart, “service[neo4j]”
#action :nothing
end

================================================================================
Error executing action create on resource
’template[/home/ubuntu/neo4j/conf/neo4j.properties]’

Chef::Mixin::Template::TemplateError

undefined method `join’ for nil:NilClass

Resource Declaration:

In /var/chef/cache/cookbooks/neo4j/recipes/default.rb

110: template “/home/ubuntu/neo4j/conf/neo4j.properties” do
111: path "/home/ubuntu/neo4j/conf/neo4j.properties"
112: source "neo4j.properties.erb"
113: owner "root"
114: group "root"
115: mode "0644"
116: variables({
117: :server_id => server_id, :neoservers => node[:neoservers]
118: })
119: #notifies :restart, "service[neo4j]"
120: #action :nothing
121: end
122:

Compiled Resource:

Declared in /var/chef/cache/cookbooks/neo4j/recipes/default.rb:110:in

`from_file’

template("/home/ubuntu/neo4j/conf/neo4j.properties") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
path "/home/ubuntu/neo4j/conf/neo4j.properties"
backup 5
source "neo4j.properties.erb"
variables {:server_id=>“20130328214635”, :neoservers=>nil}
cookbook_name "neo4j"
recipe_name "default"
owner "root"
group "root"
mode "0644"
end


#2

On Friday, March 29, 2013 at 2:20 PM, David Montgomery wrote:

Hi

In a ruby block I am trying to load a json object and passing the hash to a template. The json file exists. How to I load a json object do I cna pass to a

node[:neoservers] should look lie this [xxx.xxx.xxx.xxx (http://xxx.xxx.xxx.xxx),yyy.yyy.yyy.yyy]

ruby_block “stuff” do
block do
contents = File.read(’/tmp/cluster_ipaddress’)
existing_nodes = JSON.parse(contents)
require 'set’
neoservers = [node[:ipaddress]] + existing_nodes
neoservers = neoservers.to_set.to_a.sort
node.default[“neoservers”]=neoservers
end
action :run
end

template “/home/ubuntu/neo4j/conf/neo4j.properties” do
path "/home/ubuntu/neo4j/conf/neo4j.properties"
source "neo4j.properties.erb"
owner "root"
group "root"
mode "0644"
variables({
:server_id => server_id, :neoservers => node[:neoservers]
})
#notifies :restart, “service[neo4j]”
#action :nothing
end

You’re getting bitten by compile vs. converge time ordering issues. You can work around the issue by accessing node[:neoservers] directly in your template.


Daniel DeLeo