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

前端 未结 2 1287
独厮守ぢ
独厮守ぢ 2021-01-21 01:15

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

相关标签:
2条回答
  • 2021-01-21 01:50

    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.

    0 讨论(0)
  • 2021-01-21 01:52

    Lazy is good, but here is another approach. You may use node.run_state for your purposes.

    Here is usage example from 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
    
    0 讨论(0)
提交回复
热议问题