How can I switch user in a Capistrano task?

怎甘沉沦 提交于 2019-12-06 14:15:53

问题


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

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

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

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

Thanks in advance.

P.S. Added the output log:

$ 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

回答1:


The point is default_run_options[:pty] = true

try this

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


来源:https://stackoverflow.com/questions/12741603/how-can-i-switch-user-in-a-capistrano-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!