如何在 ruby_block 中访问节点属性和配方变量
How to access node attributes and recipe variables in ruby_block
Chef 开发的新手 :),属性的变量值在 ruby 块中似乎不可见。
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
end
action :run
end
当我对变量值进行硬编码时它起作用了:
command = 'tar -tvf /tmp/jdk-7u85-linux-x64.tar.gz | head -1 | awk \'{print $NF}\''
如何使用ruby_block中的节点属性?
此外,如何在 ruby 块之外使用 ruby 块中的变量值?有关信息,当我尝试使用 node.tar_folder 时,它没有在 ruby 块中提取的值。
提前致谢。
你需要双引号。 Ruby 中的单引号不支持 #{foo}
插值语法。
Chef 开发的新手 :),属性的变量值在 ruby 块中似乎不可见。
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
end
action :run
end
当我对变量值进行硬编码时它起作用了:
command = 'tar -tvf /tmp/jdk-7u85-linux-x64.tar.gz | head -1 | awk \'{print $NF}\''
如何使用ruby_block中的节点属性? 此外,如何在 ruby 块之外使用 ruby 块中的变量值?有关信息,当我尝试使用 node.tar_folder 时,它没有在 ruby 块中提取的值。
提前致谢。
你需要双引号。 Ruby 中的单引号不支持 #{foo}
插值语法。