如何在厨师食谱中将价值从一种资源传递到另一种资源? [英] How to pass value from one resource to another resource in chef recipe?

查看:56
本文介绍了如何在厨师食谱中将价值从一种资源传递到另一种资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



代码

  node [:oracle] [:asm] [:disks] .each_key做| disk | 
Chef :: Log.info(我在#{cookbook_name} ::#{recipe_name}中,并且当前磁盘数为#{node [:oracle] [:asm] [:test]})

bash beforeTest执行
代码< --- EOH
echo#{node [:oracle] [:asm] [:test]}
EOH
end
ruby​​_block测试当前磁盘数执行
块执行
node.set [:oracle] [:asm] [:test] =#{node [:oracle] [: asm] [:test]}。to_i + 1
结束
结束
bash test做
代码<<-EOH
echo#{node [:oracle] [:asm] [:test]}
EOH
结束
结束

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

中的值

解决方案

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

  Chef :: Log.info(我在#{cookbook_name } ::#{recipe_name}和当前磁盘数#{node [:oracle] [:asm] [:test]})

bash beforeTest会使
代码懒惰{ echo#{node [:oracle] [:asm] [:test]}}
end

ruby​​_block测试当前磁盘数执行
块执行
node.set [:oracle] [:asm] [:test] =#{node [:oracle] [:asm] [:test]}。to_i + 1
end
end

bash测试执行
代码懒惰{ echo#{node [:oracle] [:asm] [:test]}}
结尾

第一个块实际上并不需要懒惰,但是我把它放在那里,以防其他地方值也改变。 / p>

I am trying to change an attribute in one resource and want to use updated value in another resource but updated value is not getting reflected in another resources. please help me

Code

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

The value I'm trying to update is the one stored at node[:oracle][:asm][:test]

解决方案

Your problem is that the code variable is set during the compile phase of chef, before the ruby block has changed the value of your attribute. You need to add a lazy initializer around your code block.

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

The first block doesn't really need the lazy, but I threw it in there just in case the value is changing elsewhere too.

这篇关于如何在厨师食谱中将价值从一种资源传递到另一种资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆