How to load a json file into chef recipe

Hi

I have a json file…and i want to read into a recipe yet a get and error.
Wow…the file exists…

So…how to load a json file into chef recipe?

if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
zookeeper_hosts =
File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
end

template “/etc/zookeeper/conf/zoo.cfg” do
path "/etc/zookeeper/conf/zoo.cfg"
source "cloudera.zoo.cfg.erb"
owner "root"
group "root"
mode "0644"
variables :zookeeper => zookeeper_hosts
notifies :restart, resources(:service => “zookeeper-server”)
end

here is hte contents iof hte template

<% @zookeeper.each_pair do |name, ipaddress| %>
<%= “#{name}=#{ipaddress}:2888:3888” %>
<% end %>

Chef::Mixin::Template::TemplateError

undefined method `each_pair’ for nil:NilClass

Resource Declaration:

In /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb

116: template “/etc/zookeeper/conf/zoo.cfg” do
117: path "/etc/zookeeper/conf/zoo.cfg"
118: source "cloudera.zoo.cfg.erb"
119: owner "root"
120: group "root"
121: mode "0644"
122: variables :zookeeper => zookeeper_hosts
123: notifies :restart, resources(:service => “zookeeper-server”)
124: end
125:

Compiled Resource:

Declared in

/var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb:116:in `from_file’

template("/etc/zookeeper/conf/zoo.cfg") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
guard_interpreter :default
path "/etc/zookeeper/conf/zoo.cfg"
backup 5
atomic_update true
source "cloudera.zoo.cfg.erb"
variables {:zookeeper=>nil}
cookbook_name "zookeeper"
recipe_name "cloudera"
owner "root"
group "root"
mode "0644"
end

Template Context:

on line #29
27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= “#{name}=#{ipaddress}:2888:3888” %>
31: <% end %>

[2014-07-25T07:56:32+00:00] INFO: Running queued delayed notifications
before re-raising exception

Running handlers:
[2014-07-25T07:56:32+00:00] ERROR: Running exception handlers
[2014-07-25T07:56:32+00:00] ERROR: Creating JSON exception report

  • Chef::Handler::JsonFile
    Running handlers complete

[2014-07-25T07:56:32+00:00] ERROR: Exception handlers complete
[2014-07-25T07:56:32+00:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
Chef Client failed. 5 resources updated in 24.033224336 seconds
[2014-07-25T07:56:32+00:00] INFO: Sending resource update report (run-id:
d9201bdc-8c6e-4c21-9911-2c8a141c3d0c)
[2014-07-25T07:56:32+00:00] ERROR:

Chef::Mixin::Template::TemplateError (undefined method `each_pair’ for
nil:NilClass) on line #29:

27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= “#{name}=#{ipaddress}:2888:3888” %>
31: <% end %>

(erubis):29:in `block in evaluate’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in
`instance_eval’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in
`evaluate’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:126:in
`_render_template’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:112:in
`render_template’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/template/content.rb:43:in
`file_for_provider’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/file_content_management/content_base.rb:40:in
`tempfile’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:411:in
`tempfile’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:350:in
`do_contents_changes’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:121:in
`action_create’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider.rb:120:in
`run_action’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource.rb:637:in
`run_action’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:49:in
`run_action’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`block (2 levels) in converge’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`each’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`block in converge’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:98:in
`block in execute_each_resource’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call_iterator_block’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:85:in
`step’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:104:in
`iterate’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:55:in
`each_with_index’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:96:in
`execute_each_resource’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:80:in
`converge’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:345:in
`converge’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:431:in
`do_run’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:213:in
`block in run’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in
`fork’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in
`run’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:217:in
`run_chef_client’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:328:in
`block in run_application’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in
`loop’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in
`run_application’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:67:in
`run’

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/bin/chef-client:26:in
<top (required)>' /usr/bin/chef-client:23:inload’
/usr/bin/chef-client:23:in `’

Here is an example David. Not sure if this fits your use case well enough though.

cookbook_file “/etc/consul.d/common.json” do # This is the file that will be created
source “sjc-common.json” # This is the file name in chef cookbook /files/default/ that will be used
owner "consul"
group "consul"
mode "0644"
action :create_if_missing
end


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


From: David Montgomery davidmontgomery@gmail.com
Sent: Friday, July 25, 2014 1:03 AM
To: chef@lists.opscode.com
Subject: [chef] how to load a json file into chef recipe

Hi

I have a json file…and i want to read into a recipe yet a get and error. Wow…the file exists…

So…how to load a json file into chef recipe?

if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
zookeeper_hosts = File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
end

template “/etc/zookeeper/conf/zoo.cfg” do
path "/etc/zookeeper/conf/zoo.cfg"
source "cloudera.zoo.cfg.erb"
owner "root"
group "root"
mode "0644"
variables :zookeeper => zookeeper_hosts
notifies :restart, resources(:service => “zookeeper-server”)
end

here is hte contents iof hte template

<% @zookeeper.each_pair do |name, ipaddress| %>
<%= “#{name}=#{ipaddress}:2888:3888” %>
<% end %>

Chef::Mixin::Template::TemplateError

undefined method `each_pair’ for nil:NilClass

Resource Declaration:

In /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb

116: template “/etc/zookeeper/conf/zoo.cfg” do
117: path "/etc/zookeeper/conf/zoo.cfg"
118: source "cloudera.zoo.cfg.erb"
119: owner "root"
120: group "root"
121: mode "0644"
122: variables :zookeeper => zookeeper_hosts
123: notifies :restart, resources(:service => “zookeeper-server”)
124: end
125:

Compiled Resource:

Declared in /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb:116:in `from_file’

template("/etc/zookeeper/conf/zoo.cfg") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
guard_interpreter :default
path "/etc/zookeeper/conf/zoo.cfg"
backup 5
atomic_update true
source "cloudera.zoo.cfg.erb"
variables {:zookeeper=>nil}
cookbook_name "zookeeper"
recipe_name "cloudera"
owner "root"
group "root"
mode "0644"
end

Template Context:

on line #29
27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= “#{name}=#{ipaddress}:2888:3888” %>
31: <% end %>

[2014-07-25T07:56:32+00:00] INFO: Running queued delayed notifications before re-raising exception

Running handlers:
[2014-07-25T07:56:32+00:00] ERROR: Running exception handlers
[2014-07-25T07:56:32+00:00] ERROR: Creating JSON exception report

  • Chef::Handler::JsonFile
    Running handlers complete

[2014-07-25T07:56:32+00:00] ERROR: Exception handlers complete
[2014-07-25T07:56:32+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 5 resources updated in 24.033224336 seconds
[2014-07-25T07:56:32+00:00] INFO: Sending resource update report (run-id: d9201bdc-8c6e-4c21-9911-2c8a141c3d0c)
[2014-07-25T07:56:32+00:00] ERROR:

Chef::Mixin::Template::TemplateError (undefined method `each_pair’ for nil:NilClass) on line #29:

27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= “#{name}=#{ipaddress}:2888:3888” %>
31: <% end %>

(erubis):29:in block in evaluate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:ininstance_eval’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in evaluate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:126:in_render_template’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:112:in render_template' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/template/content.rb:43:infile_for_provider’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/file_content_management/content_base.rb:40:in tempfile' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:411:intempfile’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:350:in do_contents_changes' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:121:inaction_create’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider.rb:120:in run_action' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource.rb:637:inrun_action’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:49:in run_action' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:inblock (2 levels) in converge’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in each' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:inblock in converge’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:98:in block in execute_each_resource' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:incall’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in call_iterator_block' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:85:instep’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:104:in iterate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:55:ineach_with_index’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:96:in execute_each_resource' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:80:inconverge’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:345:in converge' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:431:indo_run’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:213:in block in run' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:infork’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in run' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:217:inrun_chef_client’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:328:in block in run_application' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:inloop’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in run_application' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:67:inrun’
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/bin/chef-client:26:in <top (required)>' /usr/bin/chef-client:23:inload’
/usr/bin/chef-client:23:in `’

I have python script that created the json object and saves. I then
later, trying to load the json into a hash in a recipe so it can be used in
a template.

Thanks

On Fri, Jul 25, 2014 at 8:58 PM, Justin Franks justin.franks@lithium.com
wrote:

Here is an example David. Not sure if this fits your use case well
enough though.

cookbook_file "/etc/consul.d/common.json" do # This is the file that
will be created
source "sjc-common.json" # This is the file name in chef cookbook
/files/default/ that will be used
owner "consul"
group "consul"
mode "0644"
action :create_if_missing
end


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219

From: David Montgomery davidmontgomery@gmail.com
Sent: Friday, July 25, 2014 1:03 AM
To: chef@lists.opscode.com
Subject: [chef] how to load a json file into chef recipe

Hi

I have a json file..and i want to read into a recipe yet a get and
error. Wow..the file exists.......

So..how to load a json file into chef recipe?

if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
zookeeper_hosts =
File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
end

template "/etc/zookeeper/conf/zoo.cfg" do
path "/etc/zookeeper/conf/zoo.cfg"
source "cloudera.zoo.cfg.erb"
owner "root"
group "root"
mode "0644"
variables :zookeeper => zookeeper_hosts
notifies :restart, resources(:service => "zookeeper-server")
end

here is hte contents iof hte template

<% @zookeeper.each_pair do |name, ipaddress| %>
<%= "#{name}=#{ipaddress}:2888:3888" %>
<% end %>

Chef::Mixin::Template::TemplateError

undefined method `each_pair' for nil:NilClass

Resource Declaration:

In /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb

116: template "/etc/zookeeper/conf/zoo.cfg" do
117: path "/etc/zookeeper/conf/zoo.cfg"
118: source "cloudera.zoo.cfg.erb"
119: owner "root"
120: group "root"
121: mode "0644"
122: variables :zookeeper => zookeeper_hosts
123: notifies :restart, resources(:service => "zookeeper-server")
124: end
125:

Compiled Resource:

Declared in

/var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb:116:in `from_file'

template("/etc/zookeeper/conf/zoo.cfg") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
guard_interpreter :default
path "/etc/zookeeper/conf/zoo.cfg"
backup 5
atomic_update true
source "cloudera.zoo.cfg.erb"
variables {:zookeeper=>nil}
cookbook_name "zookeeper"
recipe_name "cloudera"
owner "root"
group "root"
mode "0644"
end

Template Context:

on line #29
27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= "#{name}=#{ipaddress}:2888:3888" %>
31: <% end %>

[2014-07-25T07:56:32+00:00] INFO: Running queued delayed notifications
before re-raising exception

Running handlers:
[2014-07-25T07:56:32+00:00] ERROR: Running exception handlers
[2014-07-25T07:56:32+00:00] ERROR: Creating JSON exception report

  • Chef::Handler::JsonFile
    Running handlers complete

[2014-07-25T07:56:32+00:00] ERROR: Exception handlers complete
[2014-07-25T07:56:32+00:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
Chef Client failed. 5 resources updated in 24.033224336 seconds
[2014-07-25T07:56:32+00:00] INFO: Sending resource update report (run-id:
d9201bdc-8c6e-4c21-9911-2c8a141c3d0c)
[2014-07-25T07:56:32+00:00] ERROR:

Chef::Mixin::Template::TemplateError (undefined method `each_pair' for
nil:NilClass) on line #29:

27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= "#{name}=#{ipaddress}:2888:3888" %>
31: <% end %>

(erubis):29:in `block in evaluate'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in
`instance_eval'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in
`evaluate'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:126:in
`_render_template'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:112:in
`render_template'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/template/content.rb:43:in
`file_for_provider'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/file_content_management/content_base.rb:40:in
`tempfile'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:411:in
`tempfile'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:350:in
`do_contents_changes'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:121:in
`action_create'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider.rb:120:in
`run_action'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource.rb:637:in
`run_action'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:49:in
`run_action'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`block (2 levels) in converge'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`each'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in
`block in converge'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:98:in
`block in execute_each_resource'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in
`call_iterator_block'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:85:in
`step'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:104:in
`iterate'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:55:in
`each_with_index'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:96:in
`execute_each_resource'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:80:in
`converge'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:345:in
`converge'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:431:in
`do_run'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:213:in
`block in run'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in
`fork'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in
`run'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:217:in
`run_chef_client'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:328:in
`block in run_application'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in
`loop'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in
`run_application'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:67:in
`run'

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/bin/chef-client:26:in
<top (required)>' /usr/bin/chef-client:23:in load'
/usr/bin/chef-client:23:in `'

You're not parsing that file as JSON; just doing a File.read returns a JSON-encoded string. I'd suggest reading the Ruby stdlib documentation around the File and JSON classes.

Also, that file doesn't exist in the location you specified if it's not setting zookeeper_hosts / @zookeeper to the contents of the file. How are you saving it to the Chef file cache? If the script is available on the machine itself, have you thought about executing it with Chef and capturing/parsing the output?

Sent from my iPhone

On Jul 26, 2014, at 12:02 AM, David Montgomery davidmontgomery@gmail.com wrote:

I have python script that created the json object and saves. I then later, trying to load the json into a hash in a recipe so it can be used in a template.

Thanks

On Fri, Jul 25, 2014 at 8:58 PM, Justin Franks justin.franks@lithium.com wrote:
Here is an example David. Not sure if this fits your use case well enough though.

cookbook_file "/etc/consul.d/common.json" do # This is the file that will be created
source "sjc-common.json" # This is the file name in chef cookbook /files/default/ that will be used
owner "consul"
group "consul"
mode "0644"
action :create_if_missing
end


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219

From: David Montgomery davidmontgomery@gmail.com
Sent: Friday, July 25, 2014 1:03 AM
To: chef@lists.opscode.com
Subject: [chef] how to load a json file into chef recipe

Hi

I have a json file..and i want to read into a recipe yet a get and error. Wow..the file exists.......

So..how to load a json file into chef recipe?

if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
zookeeper_hosts = File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
end

template "/etc/zookeeper/conf/zoo.cfg" do
path "/etc/zookeeper/conf/zoo.cfg"
source "cloudera.zoo.cfg.erb"
owner "root"
group "root"
mode "0644"
variables :zookeeper => zookeeper_hosts
notifies :restart, resources(:service => "zookeeper-server")
end

here is hte contents iof hte template

<% @zookeeper.each_pair do |name, ipaddress| %>
<%= "#{name}=#{ipaddress}:2888:3888" %>
<% end %>

Chef::Mixin::Template::TemplateError

undefined method `each_pair' for nil:NilClass

Resource Declaration:

In /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb

116: template "/etc/zookeeper/conf/zoo.cfg" do
117: path "/etc/zookeeper/conf/zoo.cfg"
118: source "cloudera.zoo.cfg.erb"
119: owner "root"
120: group "root"
121: mode "0644"
122: variables :zookeeper => zookeeper_hosts
123: notifies :restart, resources(:service => "zookeeper-server")
124: end
125:

Compiled Resource:

Declared in /var/chef/cache/cookbooks/zookeeper/recipes/cloudera.rb:116:in `from_file'

template("/etc/zookeeper/conf/zoo.cfg") do
provider Chef::Provider::Template
action "create"
retries 0
retry_delay 2
guard_interpreter :default
path "/etc/zookeeper/conf/zoo.cfg"
backup 5
atomic_update true
source "cloudera.zoo.cfg.erb"
variables {:zookeeper=>nil}
cookbook_name "zookeeper"
recipe_name "cloudera"
owner "root"
group "root"
mode "0644"
end

Template Context:

on line #29
27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= "#{name}=#{ipaddress}:2888:3888" %>
31: <% end %>

[2014-07-25T07:56:32+00:00] INFO: Running queued delayed notifications before re-raising exception

Running handlers:
[2014-07-25T07:56:32+00:00] ERROR: Running exception handlers
[2014-07-25T07:56:32+00:00] ERROR: Creating JSON exception report

  • Chef::Handler::JsonFile
    Running handlers complete

[2014-07-25T07:56:32+00:00] ERROR: Exception handlers complete
[2014-07-25T07:56:32+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 5 resources updated in 24.033224336 seconds
[2014-07-25T07:56:32+00:00] INFO: Sending resource update report (run-id: d9201bdc-8c6e-4c21-9911-2c8a141c3d0c)
[2014-07-25T07:56:32+00:00] ERROR:

Chef::Mixin::Template::TemplateError (undefined method `each_pair' for nil:NilClass) on line #29:

27: # the port at which the clients will connect
28: clientPort=2181
29: <% @zookeeper.each_pair do |name, ipaddress| %>
30: <%= "#{name}=#{ipaddress}:2888:3888" %>
31: <% end %>

(erubis):29:in block in evaluate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in instance_eval'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in evaluate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:126:in _render_template'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/mixin/template.rb:112:in render_template' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/template/content.rb:43:in file_for_provider'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/file_content_management/content_base.rb:40:in tempfile' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:411:in tempfile'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:350:in do_contents_changes' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider/file.rb:121:in action_create'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/provider.rb:120:in run_action' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource.rb:637:in run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:49:in run_action' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in block (2 levels) in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in each' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:81:in block in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:98:in block in execute_each_resource' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:116:in call_iterator_block' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:85:in step'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:104:in iterate' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection/stepable_iterator.rb:55:in each_with_index'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/resource_collection.rb:96:in execute_each_resource' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/runner.rb:80:in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:345:in converge' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:431:in do_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:213:in block in run' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in fork'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/client.rb:207:in run' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:217:in run_chef_client'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:328:in block in run_application' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application/client.rb:317:in run_application' /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/lib/chef/application.rb:67:in run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.12.8/bin/chef-client:26:in <top (required)>' /usr/bin/chef-client:23:in load'
/usr/bin/chef-client:23:in `'