hamdani hamdani - 1 year ago 76
Ruby Question

How to access node attributes and recipe variables in ruby_block

New to chef development :), the variable value from an attribute does not seem to be visible in the ruby block.

installer = node['jdk']['installer']

ruby_block "tar-folder-name" do
block do
command = 'tar -tvf /tmp/#{installer}.tar.gz | head -1 | awk \'{print $NF}\''
command_out = shell_out(command)
node.default['tar_folder'] = command_out.stdout
action :run

It works when I hardcode the variable value:

command = 'tar -tvf /tmp/jdk-7u85-linux-x64.tar.gz | head -1 | awk \'{print $NF}\''

How can I use the node attributes in the ruby_block?
Moreover, how can I use a variable value from a ruby block outside ruby block? For info, when I try to use node.tar_folder, it does not have the value extracted in ruby block.

Thanks in advance.

Answer Source

You need double quotes. Single quotes in Ruby do not support the #{foo} interpolation syntax.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download