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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With