如何将Chef'执行资源'的输出放入变量中

时间:2013-04-30 22:11:43

标签: chef

我想将shell命令的输出放入变量中,以便以后在Chef配方中使用。

在bash中,我可以执行类似output=`tail -1 file.txt`的操作,然后我可以echo $output

'执行资源'可以执行此操作,以便我可以在配方中稍后使用结果吗?

2 个答案:

答案 0 :(得分:21)

当Graham的解决方案最初起作用时,我发现了Chef :: Mixin:ShellOut

ruby_block "check_curl_command_output" do
    block do
      #tricky way to load this Chef::Mixin::ShellOut utilities
      Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)      
      curl_command = 'curl --write-out %{http_code} --silent --output /dev/null '+node['url']
      curl_command_out = shell_out(curl_command)
      if curl_command_out.stdout == "302"
        ...
      else
        ...
      end
    end
    action :create
end

Chef :: Mixin:如果您需要以特定用户身份运行命令,则ShellOut特别有用(参见http://www.slideshare.net/opscode/chef-conf-windowsdougireton):

ruby_block "run_command_as" do
    block do
    Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
    add_group = shell_out("your command",
        {
          :user => "my_user",
          :password => "my_password",
          :domain => "mycorp.com"
        }
        )
    end
end

答案 1 :(得分:2)

为我工作

require 'chef/mixin/shell_out'
passenger_root = shell_out("passenger-config --root").stdout