Ruby block condition

Hi All

After some help.

I’ve two ruby_blocks. One uses aws cli to see if a snapshot has been successfull and sets a variable, the other, is “supposed” to do stuff, depending on whether that variable equals “completed”. I cannot get it to work, Can anyone shed some light on it please? I know the variable is being passed ok (hence the Log info), it’s the only_if thats the issue I believe.

First block

ruby_block ‘check_snapshot’ do
block do
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
command = “aws ec2 describe-snapshots --filter Name=volume-id,Values=’#{v_id}’ Name=status,Values=‘completed’ --region us-east-1 | grep State | awk -F: ‘{print $2}’ | sed -e 's/”//g’ -e ‘s/,//g’ -e ‘s/^ //g’"
command_out = shell_out(command)
snap_complete = command_out.stdout.to_s"Block 1 output is #{snap_complete}"

Second block

ruby_block 'unload_core' do
  block do" Block 2 output is #{snap_complete}""http_request[unload_core (#{service_name})]", :post)"bash[disable_chkconfig]", :run)"http_request[/#{node['solr']['tlog']}#{node['SLR_PRT']}/tlog]", :delete)"service[#{service_name}]", :stop)
  only_if { "completed" == "#{snap_complete}" }

Many thanks

Your variable snap_complete only exist within your first ruby_block (usual scoping of variables). Try using node.run_state['snap_complete'] as variable and all should be ok.

In case the only if still not work, you can try to use lazy evaluation for it like:

only_if lazy { "completed" == "#{node.run_state['snap_complete']}" }