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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17813592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:58:26  来源:igfitidea点击:

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

bashstdoutchefvagrant

提问by Bartvds

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.

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

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

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

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.

这也很好用,只是如果它成功完成,我永远不会看到控制台输出。但我希望看到它,以便我们可以直观地监控正在发生的事情。这不是特定于 npm。

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

我看到这个类似的问题没有具体答案: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.

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

采纳答案by Terry Wang

When you run chef - suppose we are using chef-solo, you can use -l debugto output more debug information into stdout.

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

For example: chef-solo -c solo.rb -j node.json -l debug

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

For example, a simple cookbook as below:

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

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

3 directories, 3 files

default.rb

默认.rb

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

You'll see the following output like below:

您将看到如下输出:

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

I think it contains the information you want. For example, output and the exit status of the shell script/command.

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

BTW: looks like there is a limitation (prompt for password?), you won't be able to use su

顺便说一句:看起来有限制(提示输入密码?),您将无法使用 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

回答by Tom Weiss

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.

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

回答by spuder

Use the live_streamattribute of the executeresource

使用资源的live_stream属性execute

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

Script output will be printed to the console

脚本输出将打印到控制台

   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

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

回答by Shoan

I used the following:

我使用了以下内容:

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

The flagsproperty makes the command execute like bash -x script.sh

标志属性使命令执行类似bash -x script.sh

回答by KCD

Kind of related... setting the log_location(-L) to a file prevents the chef logs (Chef::Log.info()or simply log) from going to standard out.

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

You can override this to print the full log information to stdout

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

chef-client -L /dev/stdout