Hello,
How I can use lazy variable in guards
At the beginning I set a variable to “unknown”
Within a ruby_block I set this variable to its correct value.
I have script resource that should be run if this variable is set to its correct value. I understand I have to use lazy evaluation here, but I didn’t see example on how to achieve this.
Appreciate your help
M
I believe ruby code in guards are lazily evaluated. I think your problem is more likely that you’re setting a variable in your ruby_block and expecting it to become available within your recipe’s scope. Are you using node.run_state to carry information out of your ruby_block? Something like:
ruby_block 'foo' do
block do
...
node.run_state['bar'] = 'baz'
end
end
other_resource 'biz' do
only_if { node.run_state['bar'] == 'baz' }
end
Thanks for reply,
No I am setting it to a default value outside the ruby_block.
Then inside of ruby_block I set to right value, and wanted to check in only_if
My issue is whatever I condition I put using this variable, I always get skipped due to only_if so I was guessing it has something to do with lazy evaluation but not sure how to use it.
Thanks
M
ruby_block blocks have their own scope. You can’t just set variable foo
or whatever inside a block and have it change in the recipe’s scope. That’s what node.run_state
is for as it’s available in both the recipe’s and block’s scope.