如何将价值从一种资源传递到厨师食谱中的另一种资源?

How to pass value from one resource to another resource in chef recipe?

我正在尝试更改一个资源中的属性并想在另一个资源中使用更新后的值,但更新后的值没有反映在另一个资源中。请帮助我

代码

node[:oracle][:asm][:disks].each_key do |disk|
    Chef::Log.info("I am in #{cookbook_name}::#{recipe_name} and current disk count #{node[:oracle][:asm][:test]}") 

    bash "beforeTest" do
        code <<-EOH
            echo #{node[:oracle][:asm][:test]}
        EOH
    end
    ruby_block "test current disk count" do
        block do
            node.set[:oracle][:asm][:test] = "#{node[:oracle][:asm][:test]}".to_i+1
        end
    end
    bash "test" do
        code <<-EOH
            echo #{node[:oracle][:asm][:test]}
        EOH
    end
end

我要更新的值是存储在 node[:oracle][:asm][:test]

的值

您的问题是 code 变量是在 chef 的编译阶段设置的,在 ruby 块更改您的属性值之前。您需要在代码块周围添加惰性初始化器。

Chef::Log.info("I am in #{cookbook_name}::#{recipe_name} and current disk count #{node[:oracle][:asm][:test]}") 

bash "beforeTest" do
  code lazy{ "echo #{node[:oracle][:asm][:test]}" }
end

ruby_block "test current disk count" do
  block do
    node.set[:oracle][:asm][:test] = "#{node[:oracle][:asm][:test]}".to_i+1
  end
end

bash "test" do
  code lazy{ "echo #{node[:oracle][:asm][:test]}" }
end

第一个块实际上并不需要惰性,但我把它放在那里以防其他地方的值也发生变化。

懒惰是好的,但这是另一种方法。 您可以根据自己的目的使用 node.run_state

这是来自 https://docs.chef.io/recipes.html

的用法示例
package 'httpd' do
  action :install
end

ruby_block 'randomly_choose_language' do
  block do
    if Random.rand > 0.5
      node.run_state['scripting_language'] = 'php'
    else
      node.run_state['scripting_language'] = 'perl'
    end
  end
end

package 'scripting_language' do
  package_name lazy { node.run_state['scripting_language'] }
  action :install
end