如何在Capistrano任务中切换用户? [英] How can I switch user in a Capistrano task?

查看:98
本文介绍了如何在Capistrano任务中切换用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面做了一个小的测试任务:

I made a small test task below:

set :user, "user"
set :password, "password"
set :root_password, "root password"
set :use_sudo, false
role :srv, "exmaple.com"

task :show_info do
    run "iptables -L", :shell => "su -" do |channel, stream, data|
        channel.send_data("#{root_password}\n")
    end
end

此服务器不允许我使用 sudo ,因此我必须以普通用户身份登录然后成为root用户。

This server doesn't allow me to use sudo, so I have to login as a normal user then become root.

我还尝试从surun / capistrano-executing-a-command-as-root-without-using-sudo /#comment-42918 rel = nofollow>本文,尽管它对我没有帮助...:(

I have also tried to create surun from this article, though it doesn't help me... :(

有人可以告诉我在Capistrano中切换为root后运行命令的有希望的方法吗?

Could someone please tell me the promising method to run command after switching to root in Capistrano?

预先感谢

PS 添加了输出日志:

$ cap show_info
  * executing `show_info'
  * executing "iptables -L"
    servers: ["example.com"]
    [example.com] executing command
    command finished in 000ms
failed: "su - -c 'iptables -L'" on example.com


解决方案

重点是default_run_options [:pty] = true

The point is default_run_options[:pty] = true

尝试一下

def surun(command)
  default_run_options[:pty] = true
  password = fetch(:root_password, Capistrano::CLI.password_prompt("root password: "))
  run("su - -c #{command}") do |channel, stream, output|
      channel.send_data("#{password}\n")
  end
end

这篇关于如何在Capistrano任务中切换用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆