本文介绍了如何将Chef“执行资源”的输出放入变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将shell命令的输出放入一个变量中,以备将来在厨师食谱中使用。

I'd like to put the output of a shell command into a variable for later use in a Chef recipe.

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

In bash I could do something like output=`tail -1 file.txt` and then I could echo $output

执行资源可以这样做吗,以便以后可以在配方中使用结果?

Can an 'execute resource' do this so that I can use the result later in the recipe?

推荐答案

虽然Graham的解决方案起初似乎起作用,但我发现了Chef :: Mixin:ShellOut

while Graham's solution seemed to work at first, I found out about 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):

Chef::Mixin:ShellOut is particularly useful if you need to run the command as a specific user (cf. 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

这篇关于如何将Chef“执行资源”的输出放入变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:06