I am trying to execute Ruby logic on the fly in a Chef recipe and believe that the best way to achieve this is with a block.
I am having difficulty transferring variables assigned inside the block to the main code of the chef recipe. This is what I tried:
ruby_block "Create list" do
block do
to_use ||= []
node['hosts'].each do |host|
system( "#{path}/command --all" \
" | awk '{print $2; print $3}'" \
" | grep #{host} > /dev/null" )
unless $? == 0
to_use << host
end
end
node['hosts'] = to_use.join(',')
end
action :create
end
execute "Enable on hosts" do
command "#{path}/command --enable -N #{node['hosts']}"
end
Rather than try to re-assign the chef attribute on the fly, I also tried to create new variables in the block but still can't find a way to access them in the execute
statement.
I was originally using a class and calling methods however it was compiled before the recipe and I really need the logic to be executed at runtime, during the recipe.
node['hosts'].select!
would modify it inplace. – Unvoice