在Chef资源块中运行sudo命令

时间:2015-08-13 12:02:20

标签: bash chef chef-recipe cookbook

为了简化,请考虑食谱(食谱测试)食谱中的以下块。

 79: bash 'Running sudo test sleep command' do
 80:   user 'root'
 81:   cwd '/tmp'
 82:   code <<-EOH
 83:   sudo sleep 1000
 84:   EOH
 85: end

将其作为

运行
  

&#34; chef-client -o cookbook-test&#34;

输出:

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of "bash"  "/tmp/chef-script20150813-3835-3kj758" ----
STDOUT:
STDERR: sudo: sorry, you must have a tty to run sudo
---- End output of "bash"  "/tmp/chef-script20150813-3835-3kj758" ----
Ran "bash"  "/tmp/chef-script20150813-3835-3kj758" returned 1

我添加了&#34; sudo sleep&#34;只是为了举例说明用例。在实际情况中,我们在资源块上面运行脚本,这些脚本有sudo命令。

经过一番调试后发现&#34; bash&#34;和&#34;执行&#34;资源块都没有分配tty来运行它们内部的命令。

请分享您的想法。

1 个答案:

答案 0 :(得分:1)

这就是事情:

任何人 else 都可以使用在Chef中用来运行sudo并且分配了tty的任何工具,这意味着你{{1}中的requiretty指令实际上是无用的。所以你不妨删除它,省去解决它的麻烦。

话虽如此,这里有一些方法可以解决这个问题:

  1. 您是否可以在没有密码的情况下sudoersssh?您可以使用localhost

  2. 您可以使用ssh -tt localhost sudo somecomand ...工具,该工具用于控制面向终端的程序。像expect

  3. 之类的东西
  4. 您可以expect -c "spawn sudo somecommand; interact"使用screen

相关问题