Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display the output of a Opscode Chef bash command in my console?

I use Vagrant to spawn a standard "precise32" box and provision it with Chef so I can test my Node.js code on Linux when I work on a Windows machine. This works fine.

I also have this bash command so it auto installs my npm modules:

bash "install npm modules" do
  code <<-EOH
    su -l vagrant -c "cd /vagrant && npm install"
  EOH
end

This also works fine except that I never see the console output if it completes successfully. But I'd like to see it so we can visually monitor what is going on. This is not specific to npm.

I see this similar question with no concrete answers: Vagrant - how to print Chef's command output to stdout?

I tried specifying flags but I'm a terrible linux/ruby n00b and create either errors or no output at all, so please edit my snippet with an example of your solution.

like image 297
Bartvds Avatar asked Jul 23 '13 14:07

Bartvds


People also ask

How do I turn off output in bash?

To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.


1 Answers

I try to use logging when possible, but I've found that in some scenarios seeing the output is important. Here's the short version of the way I do it. Substituting the execute resource for the bash resource also works fine. Both standard error and standard output go into the file.

results = "/tmp/output.txt"
file results do
    action :delete
end

cmd = "ls  /"
bash cmd do
    code <<-EOH
    #{cmd} &> #{results}
    EOH
end

ruby_block "Results" do
    only_if { ::File.exists?(results) }
    block do
        print "\n"
        File.open(results).each do |line|
            print line
        end
    end
end
like image 193
Tom Weiss Avatar answered Oct 21 '22 23:10

Tom Weiss