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

Bartvds picture Bartvds · Jul 23, 2013 · Viewed 35.6k times · Source

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.

Answer

Tom Weiss picture Tom Weiss · Nov 6, 2013

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