Sahil Grover
Sahil Grover

Reputation: 1915

How to create roles and run capistrano tasks for specific roles?

Here is my production.rb in config/deploy

Instance Details
server '198.61.179.237', :web, :app, :db, primary: true
server '198.61.228.160', :file_server

# Rails Environment
set :rails_env, 'production'

And from deploy.rb

namespace :check do
  task :function_1, :roles => :web do
    puts 'function_1'
  end
  task :function_2, :roles => :file_server do
    puts 'filesssss'
  end
end

But when I try doing

cap HOSTS=198.61.228.160 production check:function_2
cap HOSTS=198.61.228.160 production check:function_1

cap HOSTS=198.61.179.237 production check:function_2
cap HOSTS=198.61.179.237 production check:function_1

Everyone of them gives respective output. But as per the declaration

function_1 should work only for :role => :web and similarly function_2 should work only for :role => :file_server.

Where I am going wrong ? What is the correct way to approach ?

Upvotes: 3

Views: 1350

Answers (1)

Mike
Mike

Reputation: 346

I believe what you want is cap HOSTFILTER=198.61.228.160 function_2 or cap HOSTFILTER=198.61.179.237 function_1

It's because the HOSTFILTER checks for the intersect of all the servers with the functions role and the server you're looking for. A great explanation can be found here by Pete Hodgson

Also we can see this because of the manual:

    $ cap -H

       HOSTS
            Execute the tasks against this comma-separated list of hosts.
            Effectively, this makes the host(s) part of every roles.

       HOSTFILTER
            Execute tasks against this comma-separated list of host, 
            but only if the host has the proper role for the task.

       HOSTROLEFILTER
            Execute tasks against the hosts in this comma-separated list of roles,
            but only if the host has the proper role for the task.

       ROLES
            Execute tasks against this comma-separated list of roles.  Hosts which
            do not have the right roles will be skipped.

Upvotes: 2

Related Questions