我必须在循环中运行execute
资源。例如:
ruby_block "iterate over states" do
block do
dir = "#{node['config']['location']}/config-script/properties/#{node['config']['payer']}"
state_names = Dir.entries(dir).select do |file|
File.directory?("#{dir}/#{file}") and ! file.start_with? "."
end
state_names = [""] if state_names.empty?
state_names.each do |state|
run_context = Chef::RunContext.new(node, {}, nil)
r = Chef::Resource::Execute.new("config rest", run_context)
r.cwd "#{node['config']['location']}/config-script"
r.command "echo"
r.run_action(:create)
end
end
end
这里我试图为目录中的每个文件夹运行一个执行命令。但是我遇到了以下错误:
ruby_block("iterate over states") do
action [:create]
retries 0
retry_delay 2
block_name "iterate over states"
cookbook_name "config"
recipe_name "rest"
block #<Proc:0x96e0b3c@/var/chef/cache/cookbooks/config/recipes/rest.rb:11>
end
[2013-12-20T14:26:55+00:00] ERROR: Running exception handlers
[2013-12-20T14:26:55+00:00] ERROR: Exception handlers complete
[2013-12-20T14:26:55+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 1 resources updated
[2013-12-20T14:26:55+00:00] ERROR: ruby_block[iterate over states] (config::rest line 10) had an error: NoMethodError: undefined method `resource_action_start' for nil:NilClass
[2013-12-20T14:26:55+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
我是厨师新手,无法调试此错误。
修改
我观察到以下堆栈跟踪/var/chef/cache/chef-stacktrace.out
Generated at 2013-12-20 14:45:12 +0000
NoMethodError: ruby_block[iterate over states] (config::rest line 9) had an error: NoMethodError: undefined method `resource_action_start' for nil:NilClass
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource.rb:608:in `run_action'
/var/chef/cache/cookbooks/config/recipes/rest.rb:21:in `block (3 levels) in from_file'
/var/chef/cache/cookbooks/config/recipes/rest.rb:16:in `each'
/var/chef/cache/cookbooks/config/recipes/rest.rb:16:in `block (2 levels) in from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/provider/ruby_block.rb:33:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/provider/ruby_block.rb:33:in `block in action_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/mixin/why_run.rb:52:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/mixin/why_run.rb:52:in `add_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/provider.rb:149:in `converge_by'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/provider/ruby_block.rb:32:in `action_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/provider.rb:114:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource.rb:625:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/runner.rb:49:in `run_action'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/runner.rb:81:in `block (2 levels) in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/runner.rb:81:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/runner.rb:81:in `block in converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection.rb:98:in `block in execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/resource_collection.rb:96:in `execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/runner.rb:80:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/client.rb:433:in `converge'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/client.rb:500:in `do_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/client.rb:199:in `block in run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/client.rb:193:in `fork'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/client.rb:193:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/application.rb:208:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/application/client.rb:312:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/application/client.rb:304:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/application/client.rb:304:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/lib/chef/application.rb:66:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.8.2/bin/chef-client:26:in `<top (required)>'
/usr/bin/chef-client:23:in `load'
/usr/bin/chef-client:23:in `<main>'
答案 0 :(得分:4)
您不需要创建run_context,配方中已经提供了run_context
而不是这段代码
run_context = Chef::RunContext.new(node, {}, nil)
r = Chef::Resource::Execute.new("config rest", run_context)
r.cwd "#{node['config']['location']}/config-script"
r.command "echo"
r.run_action(:create)
你可能有
r = Chef::Resource::Execute.new("config rest", run_context)
r.cwd "#{node['config']['location']}/config-script"
r.command "echo"
r.run_action(:create)
我认为这可以解决您的问题