17

我使用 Vagrant 生成一个标准的“precise32”框并使用 Chef 进行配置,这样当我在 Windows 机器上工作时,我可以在 Linux 上测试我的 Node.js 代码。这工作正常。

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

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

这也可以正常工作,只是如果成功完成,我永远不会看到控制台输出。但我想看看它,这样我们就可以直观地监控正在发生的事情。这不是特定于 npm 的。

我看到这个没有具体答案的类似问题:Vagrant - how to print Chef's command output to stdout?

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

4

5 回答 5

16

我尽可能尝试使用日志记录,但我发现在某些情况下查看输出很重要。这是我这样做的简短版本。将执行资源替换为 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
于 2013-11-06T18:48:45.817 回答
13

使用资源的live_stream属性execute

execute 'foo' do
  command 'cat /etc/hosts'
  live_stream true
  action :run
end

脚本输出将打印到控制台

   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

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

于 2017-02-08T01:09:22.830 回答
9

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

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

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

$ tree 
.
├── cookbooks
│   └── main
│       └── recipes
│           └── default.rb
├── node.json
└── solo.rb

3 directories, 3 files

默认.rb

bash "echo something" do
   code <<-EOF
     echo 'I am a chef!'
   EOF
end

您将看到如下输出:

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

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

顺便说一句:看起来有限制(提示输入密码?),您将无法使用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
于 2013-07-24T05:53:58.797 回答
3

我使用了以下内容:

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

flags属性使命令执行如下bash -x script.sh

于 2016-11-22T12:28:01.177 回答
0

有点相关...将log_location( -L) 设置为文件可防止厨师日志(Chef::Log.info()或简单地log)进入标准输出。

您可以覆盖它以将完整的日志信息打印到标准输出

chef-client -L /dev/stdout
于 2015-10-28T20:37:54.310 回答