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
Chef::Log.info"Block 1 output is #{snap_complete}"
end
end

Second block

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

Many thanks
Mark

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']}" }