Capistrano 3 sudo task

前端 未结 5 1686
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 15:48

I want to write a recipe with Capistrano 3 executing a task on the remote server with sudo.

With Capistrano 2 this could be done for example:

default_run         


        
5条回答
  •  执笔经年
    2021-02-01 16:01

    I usually write like this:

    task :hello do
      on roles(:all) do |host|
        execute :sudo, :cp, '~/something', '/something'
      end
    end
    

    Edit

    Capistrano 3 does not support sudo with password.

    However, I created a small gem, which enables you to use sudo with password in Capistrano 3 task.

    Add sshkit-sudo to your application's Gemfile:

    # Gemfile
    gem 'sshkit-sudo'
    

    And require 'sshkit/sudo' in you Capfile:

    # Capfile
    require 'sshkit/sudo'
    

    Now, you can execute a command with sudo as follows:

    task :hello do
      on roles(:all) do
        sudo :cp, '~/something', '/something'
      end
    end
    

提交回复
热议问题