【问题标题】:How to run sh scripts by Chef and show echo output如何通过 Chef 运行 sh 脚本并显示 echo 输出
【发布时间】:2021-02-24 19:40:07
【问题描述】:

我遇到了 Chef 和 sh 脚本的问题。我的 sh 脚本做了一些事情,对于每一步,它都会回显一条消息,表明某事是否成功完成。我想在 Chef 中运行这个脚本,但它不起作用,我在日志中看不到任何内容,除了脚本运行成功,这是不正确的。这是我的做法:

cookbook_file "/tmp/myLib.rpm" do
source "myLib.rpm"
mode '0775'
action :create
end

cookbook_file "/tmp/install.sh" do
    source "install.sh"
    mode '0755'
    action :create
end

execute "installing my lib" do
    command "sh /tmp/install.sh"
    user 'root'
    live_stream true
    action :run
end

脚本已成功完成,但在日志中我只能看到:

Loading cookbooks [security@0.0.1]
INFO: *** Chef 12.18.31 ***
INFO: Storing updated cookbooks/security/recipes/install-mylib.rb in the cache.
INFO: Storing updated cookbooks/security/metadata.rb in the cache.
INFO: Storing updated cookbooks/security/files/default/install.sh in the cache.
INFO: Storing updated cookbooks/security/files/default/myLib.rpm in the cache.
INFO: Processing cookbook_file[/tmp/myLib.rpm] action create (security::install-mylib line 1)
INFO: Processing cookbook_file[/tmp/install.sh] action create (security::install-mylib line 7)
INFO: cookbook_file[/tmp/install.sh] created file /tmp/install.sh
INFO: cookbook_file[/tmp/install.sh] updated file contents /tmp/install.sh
INFO: cookbook_file[/tmp/install.sh] mode changed to 755
INFO: Processing execute[installing my lib] action run (security::install-mylib line 13)
INFO: execute[installing my lib] ran successfully

我看不到来自 install.sh 的任何回声,并且此脚本的工作尚未完成。当我直接在机器上运行这个脚本时,一切正常。

任何帮助或想法将不胜感激。

【问题讨论】:

  • log_level 设置为什么?
  • log_level 设置为调试

标签: bash chef-infra aws-opsworks


【解决方案1】:

运行脚本的更好选择是使用script resource。虽然execute 资源也应该可以正常工作。

尝试使用script 资源运行您的脚本,如下所示:

script 'installing my lib' do
  code '/tmp/install.sh'
  user 'root'
  interpreter 'bash'
  action :run
end

【讨论】:

  • 我得到了相同的结果 - 所有步骤都成功运行,但脚本中的工作没有完成:(