Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano: call 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 can I execute this task with arguments but not from command-line. The next implementation would 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 is not working. One way to make this work is to set the global variables every time before executing the task, but it's not an elegant solution.

Can you tell me a better way to implement this?

PS I don't know ruby, I'm just using capistrano for deployment

like image 605
Andrei Canta Avatar asked Nov 13 '22 06:11

Andrei Canta


1 Answers

Tasks don't take arbitrary arguments. However you can include any ruby code in your Capfile. So you can define a method that calls run:

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

You can also use an iterator:

task :change_passwords do
  [
   ['user1', 'oldpass', 'newpass'],
   ['user2', 'oldpass', 'newpass'],
   ['user3', 'oldpass', 'newpass']
  ].each do |ary|
    mysql_change_password(*ary)
  end
end
like image 197
bjmllr Avatar answered Nov 15 '22 05:11

bjmllr