Capistrano: challenge task with arguments (internally)

I have this code

namespace :mysql do
  task :change_password do
    run "mysqladmin -u #{user} -p#{old} password #{new}"
  end
end

I want to use this to change the password for multiple users. How to accomplish this task with arguments, but not from the command line. The next implementation will be fantastic.

task :change_passwords do
  mysql.change_password('user1', 'oldpass', 'newpass');
  mysql.change_password('user2', 'oldpass', 'newpass');
  mysql.change_password('user3', 'oldpass', 'newpass');
end

Unfortunately this does not work. One way to do this work is to set global variables every time before completing a task, but this is not an elegant solution.

Can you tell me the best way to implement this?

PS I do not know ruby, I just use capistrano to deploy

+5
source share
1 answer

. Capfile. , , run:

def mysql_change_password(user, old, new)
  run "mysqladmin -u #{user} -p#{old} password #{new}"
end

:

task :change_passwords do
  [
   ['user1', 'oldpass', 'newpass'],
   ['user2', 'oldpass', 'newpass'],
   ['user3', 'oldpass', 'newpass']
  ].each do |ary|
    mysql_change_password(*ary)
  end
end
+1

All Articles