如何通过Chef运行sh脚本并显示回显输出

问题描述

我在使用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的任何回音,并且此脚本的工作未完成。当我直接在计算机上运行此脚本时,一切正常。

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

解决方法

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

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

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