【问题标题】:How to access node attributes and recipe variables in ruby_block如何在 ruby​​_block 中访问节点属性和配方变量
【发布时间】:2017-02-05 12:04:06
【问题描述】:

新厨师开发:),属性的变量值在 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​​ 块中提取的值。

提前致谢。

【问题讨论】:

  • attributes/default.rb文件中设置的属性为default['jdk']['installer'] = 'jdk-7u85-linux-x64'

标签: ruby chef-infra chef-recipe


【解决方案1】:

你需要双引号。 Ruby 中的单引号不支持 #{foo} 插值语法。

【讨论】:

  • 太好了,非常感谢。如何访问 ruby​​ 块之外的 ruby​​ 块属性值以供以后在配方中使用?我试过 #{node.default['tar_folder']} 但它是空的。
  • 您可能正在运行编译 bs 收敛问题。查看coderanger.net/two-pass
猜你喜欢
  • 1970-01-01
  • 2019-09-23
  • 2012-03-27
  • 1970-01-01
  • 2011-11-23
  • 2018-03-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多