【问题标题】:How to pass value from one resource to another resource in chef recipe?如何在厨师食谱中将价值从一种资源传递到另一种资源?
【发布时间】:2015-06-25 10:32:55
【问题描述】:

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

代码

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]的值

【问题讨论】:

    标签: ruby chef-infra recipe


    【解决方案1】:

    您的问题是 code 变量是在厨师的编译阶段设置的,在 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
    

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

    【讨论】:

    • 我仍然收到如下错误:NoMethodError ------------- from_file'资源声明中的未定义方法echo' for Chef::Resource::Bash Cookbook Trace: --------------- /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb:3:in block(2级):---- ----------------- # In /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb 1: bash "beforeTest" do 2: code lazy{ 3: echo "#{node[:oracle][:asm][:test]}" 4: } 5: 结束
    • 对不起,那只是因为命令不在引号中。我会解决的。
    • 为什么要在 ruby​​_block 中使用惰性?
    • @TejayCardon 我被震撼了。已经使用 Chef 3 - 4 年,并尝试了一些非常复杂的方法来将输出从一个资源转换为另一个资源(尤其是在加密数据包周围)。这解决了很多问题。谢谢!!
    • @Brett 没问题。 Lazy 概念是对 Chef 的巨大补充,它确实使更多事情成为可能。现在,如果我能在食谱范围之外使用 Lazy 就好了....
    【解决方案2】:

    懒惰是好的,但这是另一种方法。 您可以将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
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多