0

I am trying to increment the value and use in another resource dynamically in recipe but still failing to do that.

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

However I'm still getting the error bellow:

NoMethodError ------------- undefined method echo' for Chef::Resource::Bash 

Cookbook Trace: --------------- 
/var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb:3:in block (2 levels) in from_file' 
Resource Declaration: --------------------- 
# In /var/chef/cache/cookbooks/Oracle11G/recipes/testSplit.rb 
1: bash "beforeTest" do 
2: code lazy{ 
3: echo "#{node[:oracle][:asm][:test]}" 
4: } 
5: end

Please can you help how lazy should be used in bash? If not lazy is there any other option?

Tensibai
  • 15,557
  • 1
  • 37
  • 57
SASI
  • 475
  • 2
  • 7
  • 16

2 Answers2

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

You should quote the command for the interpolation to work; if not, ruby would search for an echo command, which is unknown in ruby context (thus the error you get in log).

Warning: lazy has to be for the whole resource attribute; something like this WON'T work:

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

The lazy evaluation takes a block of ruby code, as decribed here

You may have a better result with the log resource like this:

log "print before" do
  message lazy { "node asm test is #{node[:oracle][:asm][:test]}" }
end
Community
  • 1
  • 1
Tensibai
  • 15,557
  • 1
  • 37
  • 57
0

I've been drilling my head solving this problem until I came up with lambda expressions. But yet just using lambda didn't help me at all. So I thought of using both lambda and lazy evaluation. Though lambda is already lazy loading, when compiling chef recipe's, the resource where you call the lambda expression is still being evaluated. So to prevent it to being evaluated (somehow), I've put it inside a lazy evaluation string.

The lambda expression

app_version = lambda{`cat version`}

then the resource block

file 'tmp/validate.version' do
    user 'user'
    group 'user_group'
    content lazy { app_version.call }
    mode '0755'
end

Hope this can help others too :) or if you have some better solution please do let me know :)

Rodel
  • 147
  • 1
  • 6