88

What is the purpose and function of "roles" in a Capistrano recipe? When I look at sample recipes, I often see something like this:

role :app, 'somedomain.com'
role :web, 'somedomain.com'
role :db,  'somedomain.com', :primary => true

So it looks like a role is basically a server where Capistrano executes commands. If that's the case, then why would it be called a "role" rather than a "host" or "server"?

In the above example, what is the difference between the :app and :web roles?

What does the :primary => true option do?

Ethan
  • 57,819
  • 63
  • 187
  • 237

2 Answers2

67

Roles allow you to write capistrano tasks that only apply to certain servers. This really only applies to multi-server deployments. The default roles of "app", "web", and "db" are also used internally, so their presence is not optional (AFAIK)

In the sample you provided, there is no functional difference.

The ":primary => true" is an attribute that allows for further granularity in specifying servers in custom tasks.

Here is an example of role specification in a task definition:

task :migrate, :roles => :db, :only => { :primary => true } do
  # ...
end

See the capistrano website @ https://github.com/capistrano/capistrano/wiki/2.x-DSL-Configuration-Roles-Role for a more extensive explanation.

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
codeprimate
  • 1,376
  • 10
  • 6
  • 2
    Also note: your line should be `:only => { :primary => true }` I think...or the `role` command in the question should use `:master => true` instead. I believe these attributes are entirely free-form. – docwhat Dec 27 '10 at 14:16
  • The link doesn't work anymore. – ZedTuX Feb 20 '22 at 19:37
3

The ":primary => true" option indicates that the database server is primary server. This is important for when you want to use replication with MySQL, for example. It allows you to create another mirrored database server that can be used for automatic failover. It's also used for deciding on which database server the model migrations should be run (as those changes will be replicated to the failover servers). This link clarifies it a bit more: https://github.com/capistrano/capistrano/wiki/2.x-from-the-beginning#back-to-configuration

Bobby Wallace
  • 203
  • 1
  • 5