How can I put the output of a Chef 'execute resource' into a variable

前端 未结 2 1750
傲寒
傲寒 2020-12-08 10:41

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

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

相关标签:
2条回答
  • 2020-12-08 11:17

    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 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
    
    0 讨论(0)
  • 2020-12-08 11:25

    Works for me

    passenger_root = shell_out("passenger-config --root").stdout
    
    0 讨论(0)
提交回复
热议问题