Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Capistrano task that performs different tasks based on role

I'm looking for a way to call a single Capistrano task to perform different things to different roles. Is Capistrano able to do this, or do I have write a specific task for each role?

like image 449
Michael Avatar asked Apr 15 '09 22:04

Michael


3 Answers

The standard way to do this in Capistrano:

task :whatever, :roles => [:x, :y, :z] do
  x_tasks
  y_tasks
  z_tasks
end

task :x_tasks, :roles => :x do
  #...
end

task :y_tasks, :roles => :y do
  #...
end

task :z_tasks, :roles => :z do
  #...
end

So yes, you do need to write separate tasks, but you can call them from a parent task and they will filter appropriately.

like image 58
Sarah Mei Avatar answered Oct 21 '22 06:10

Sarah Mei


Actually no:

% cat capfile 
server 'localhost', :role2
task :task1, :roles=>:role1 do
  puts 'task1'
end
task :task2 do
  task1
end

% cap task2  
  * executing `task2'
  * executing `task1'
task1

The :roles param is passed further to run command etc but does not seem to affect whether the task is actually fired.

Sorry, didn't find the way to put a comment on comment so I've written it here.

like image 42
timurb Avatar answered Oct 21 '22 07:10

timurb


You can also do

task :foo do
    run "command", :roles => :some_role
    upload "source", "destination", :roles => :another_role
end
like image 42
Matěj Koubík Avatar answered Oct 21 '22 07:10

Matěj Koubík