如何在控制台中显示Opscode Chef bash命令的输出?

Bar*_*vds 16 bash stdout chef-infra vagrant

我使用Vagrant生成一个标准的"exact32"框并使用Chef配置它,这样我就可以在Windows机器上测试我的Node.js代码了.这很好用.

我也有这个bash命令,所以它自动安装我的npm模块:

bash "install npm modules" do
  code <<-EOH
    su -l vagrant -c "cd /vagrant && npm install"
  EOH
end
Run Code Online (Sandbox Code Playgroud)

这也可以正常工作,除非我成功完成后才会看到控制台输出.但我希望看到它,以便我们可以直观地监控正在发生的事情.这不是npm特有的.

我看到这个类似的问题没有具体答案:Vagrant - 如何将Chef的命令输出打印到stdout?

我尝试指定标志,但我是一个糟糕的linux/ruby​​ n00b并且根本没有创建错误或输出,所以请使用您的解决方案示例编辑我的代码段.

Tom*_*iss 14

我尝试尽可能使用日志记录,但我发现在某些情况下看到输出很重要.这是我这样做的简短版本.将执行资源替换为bash资源也可以正常工作.标准错误和标准输出都进入文件.

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
Run Code Online (Sandbox Code Playgroud)

  • 这对汤姆有用.您也可以使用print File.read(结果)代替循环. (2认同)

spu*_*der 10

使用资源的live_stream属性execute

execute 'foo' do
  command 'cat /etc/hosts'
  live_stream true
  action :run
end
Run Code Online (Sandbox Code Playgroud)

脚本输出将打印到控制台

   Starting Chef Client, version 12.18.31
   resolving cookbooks for run list: ["apt::default", "foobar::default"]
   Synchronizing Cookbooks:
   Converging 2 resources
   Recipe: foobar::default
     * execute[foo] action run
       [execute] 127.0.0.1  default-ubuntu-1604 default-ubuntu-1604
          127.0.0.1 localhost
          127.0.1.1 vagrant.vm  vagrant
          ::1     localhost ip6-localhost ip6-loopback
          ff02::1 ip6-allnodes
          ff02::2 ip6-allrouters
       - execute cat /etc/hosts
Run Code Online (Sandbox Code Playgroud)

https://docs.chef.io/resource_execute.html

  • 我个人认为这是对这个问题的最佳答案。我猜这不是最常见的答案,因为当问这个问题或类似问题时,不支持“ live_stream”。 (2认同)

Ter*_*ang 9

当你运行chef时 - 假设我们正在使用chef-solo,你可以使用-l debug输出更多调试信息到stdout.

例如: chef-solo -c solo.rb -j node.json -l debug

例如,一个简单的食谱如下:

$ tree 
.
??? cookbooks
?   ??? main
?       ??? recipes
?           ??? default.rb
??? node.json
??? solo.rb

3 directories, 3 files
Run Code Online (Sandbox Code Playgroud)

default.rb

bash "echo something" do
   code <<-EOF
     echo 'I am a chef!'
   EOF
end
Run Code Online (Sandbox Code Playgroud)

您将看到以下输出,如下所示:

Compiling Cookbooks...
[2013-07-24T15:49:26+10:00] DEBUG: Cookbooks to compile: [:main]
[2013-07-24T15:49:26+10:00] DEBUG: Loading Recipe main via include_recipe
[2013-07-24T15:49:26+10:00] DEBUG: Found recipe default in cookbook main
[2013-07-24T15:49:26+10:00] DEBUG: Loading from cookbook_path: /data/DevOps/chef/cookbooks
Converging 1 resources
[2013-07-24T15:49:26+10:00] DEBUG: Converging node optiplex790
Recipe: main::default
  * bash[echo something] action run[2013-07-24T15:49:26+10:00] INFO: Processing bash[echo something] action run (main::default line 4)
[2013-07-24T15:49:26+10:00] DEBUG: Platform ubuntu version 13.04 found
I am a chef!
[2013-07-24T15:49:26+10:00] INFO: bash[echo something] ran successfully

    - execute "bash"  "/tmp/chef-script20130724-17175-tgkhkz"

[2013-07-24T15:49:26+10:00] INFO: Chef Run complete in 0.041678909 seconds
[2013-07-24T15:49:26+10:00] INFO: Running report handlers
[2013-07-24T15:49:26+10:00] INFO: Report handlers complete
Chef Client finished, 1 resources updated
[2013-07-24T15:49:26+10:00] DEBUG: Forked child successfully reaped (pid: 17175)
[2013-07-24T15:49:26+10:00] DEBUG: Exiting
Run Code Online (Sandbox Code Playgroud)

我认为它包含您想要的信息.例如,shell脚本/命令的输出和退出状态.

BTW:看起来有限制(提示密码?),你将无法使用 su

[2013-07-24T15:46:10+10:00] INFO: Running queued delayed notifications before re-raising exception
[2013-07-24T15:46:10+10:00] DEBUG: Re-raising exception: Mixlib::ShellOut::ShellCommandFailed - bash[echo something] (main::default line 4) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20130724-16938-1jhil9v" ----
STDOUT: 
STDERR: su: must be run from a terminal
---- End output of "bash"  "/tmp/chef-script20130724-16938-1jhil9v" ----
Ran "bash"  "/tmp/chef-script20130724-16938-1jhil9v" returned 1
Run Code Online (Sandbox Code Playgroud)