3

is there any way to switch the user (to root or so) while deploying with capistrano without closing the current session (session is started with ssh keys)?

So i want to start the deployment with user foo and then change to root and then execute some commands.

Thanks for your answers!

sdepold
  • 6,171
  • 2
  • 41
  • 47
  • I hope this answers your question: http://stackoverflow.com/questions/22054076/capistrano-3-change-ssh-options-inside-task/23569541#23569541 – activars May 09 '14 at 22:46

4 Answers4

6

You can use the capistrano sudo helper:

task :some_task_as_root do
  run "#{sudo} some_command"
end

you can even specify a different user:

task :some_task_as_other_user do
  run "#{sudo :as => 'other_user'} some_command"
end

The sudo helper will take care of getting the password from you if necessary and pass it along correctly.

severin
  • 10,148
  • 1
  • 39
  • 40
1

You don't say where you want to run the commands as root. On the local system? On the remote system?

Each "run" sort of command in capistrano uses a separate new ssh connection. As stated above, "sudo" is the official way to run one set of statements as root.

If you're comfortable giving Root an ssh key to be able to log in directly via ssh, you can specify a user to ssh over as. I wouldn't recommend it, though.

In a Solaris environment without "sudo" (sigh), we use pfexec instead.

JBB
  • 4,543
  • 3
  • 24
  • 25
0

I believe sudo is what you need

Victor Moroz
  • 9,167
  • 1
  • 19
  • 23
-1

your user should be sudoers groups before.

then use it:

task :specific_sudo_take do
  run 'sudo -c sh "whoami' #put your sudo command here
end
Anatoly
  • 15,298
  • 5
  • 53
  • 77