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

SAS*_*ASI 3 ruby recipe chef-infra

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

代码

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
Run Code Online (Sandbox Code Playgroud)

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

Tej*_*don 5

您的问题是该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
Run Code Online (Sandbox Code Playgroud)

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