【问题标题】:How can I display the output of a Opscode Chef bash command in my console?如何在控制台中显示 Opscode Chef bash 命令的输出?
【发布时间】:2013-07-22 17:22:07
【问题描述】:

我使用 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,并且会产生错误或根本没有输出,所以请使用您的解决方案示例编辑我的 sn-p。

【问题讨论】:

    标签: bash stdout chef-infra vagrant


    【解决方案1】:

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

    【讨论】:

    • 这对汤姆有效。除了循环,您还可以使用 print File.read(results)。
    • 另一个替代循环的选项:Chef::Log.info(IO.read(results))
    • 好评论 Joe,Ruby 不是我的第一、第二或第三编程语言,所以我不知道捷径。
    【解决方案2】:

    使用execute 资源的live_stream 属性

    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

    【讨论】:

    • 我试过live_stream true。但这并不适合我。我已在stackoverflow.com/questions/51307728/… 中发布了详细信息。有什么想法可能出了什么问题?
    • 我个人认为这是这个问题的最佳答案。我猜这不是最佳答案或被接受的答案,因为在提出此问题或类似问题时不支持live_stream
    【解决方案3】:

    当您运行 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
    

    default.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
    

    【讨论】:

    • 我正在运行 chef-solo 11.8.0,但使用 bash 脚本中的 echo 进行日志级别调试没有得到任何输出。这有改变吗?
    • nvm,因为我是通过 vagrant 运行 chef-solo
    • @JoshNankin 你是如何让 vagrant 输出回声的?
    【解决方案4】:

    我使用了以下内容:

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

    flags 属性使命令像bash -x script.sh 一样执行

    【讨论】:

      【解决方案5】:

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

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

      chef-client -L /dev/stdout
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-23
        • 2014-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        相关资源
        最近更新 更多