Ruby 在迭代器中运行执行资源
我必须在循环中运行Ruby 在迭代器中运行执行资源,ruby,chef-infra,Ruby,Chef Infra,我必须在循环中运行执行资源。例如: 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?(
执行资源。例如:
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
在这里,我试图为目录中的每个文件夹运行executeby命令。但是,我遇到了以下错误:
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)
您不需要创建run_上下文,配方中已经提供了run_上下文
而不是这个代码
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)
我认为这将解决您的问题您不需要创建run\u上下文,配方中已经提供了run\u上下文
而不是这个代码
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)
我想这会解决你的问题你正在传递nil
这里Chef::RunContext.new(node,{},nil)
你确定这是有效的吗?我必须得到一个新的RunContext并读取它,传递null是可以的。你可能不想创建一个新的run\u上下文。当前运行上下文在配方DSL中作为“运行上下文”提供,您正在传递nil
hereChef::RunContext.new(node,{},nil)
您确定这是有效的吗?我必须获取一个新的运行上下文,并读取它,确保传递null是可以的。您可能不想创建新的运行上下文。当前运行上下文在配方DSL中作为“运行上下文”可用