【问题标题】:Run sudo commands in Chef resource block在 Chef 资源块中运行 sudo 命令
【发布时间】:2015-08-13 21:39:34
【问题描述】:

为简化起见,请考虑遵循食谱(食谱测试)食谱中的块。

 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

运行这个

“厨师客户-o 食谱测试”

输出:

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

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

经过一些调试发现“bash”和“execute”资源块都没有分配tty来运行它们里面的命令。

请分享你的想法。

【问题讨论】:

  • 检查您的 sudoers 配置文件(通常是 /etc/sudoers)并检查 Defaults 中是否有 requiretty 条目。如果是这样,您可以删除它,并且您的上述示例可以工作。
  • 是的。检查了。是的,它确实提到了条目。但不幸的是,目前还不能删除它。
  • 寻找一种不改变系统范围配置的方法。
  • bash 或执行资源是否有任何属性可以设置为在分配的 tty 中运行命令?到目前为止,我还没有在厨师文档中找到任何内容。
  • 你已经在资源上设置了user 'root',所以它将以root身份运行。为什么还需要 sudo?

标签: bash chef-infra chef-recipe cookbook


【解决方案1】:

事情是这样的:

您可以在 Chef 中使用任何工具来运行带有分配的 tty 的 sudo 也可以被任何人使用else,这意味着您的 sudoers 中的 requiretty 指令实际上是无用的。因此,您不妨将其删除,省去解决它的麻烦。

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

  1. 您可以在没有密码的情况下将ssh 转至localhost 吗?你可以使用ssh -tt localhost sudo somecomand ...

  2. 您可以使用expect 工具,该工具专为控制面向终端的程序而设计。类似expect -c "spawn sudo somecommand; interact"

  3. 您可以使用screen,类似于screen sudo somecommand

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多