Maco
Maco

Reputation: 21

How to get the current role name in capistrano tasks

I have the following piece of code:

task :back_up, :roles => [:server1, :server2] do

I want to know the role name server1 or server2 where the tasks is being executed. I need this to generate dynamic strings based on the server end

Is this possible?

Upvotes: 2

Views: 1661

Answers (1)

Dmitry Sokurenko
Dmitry Sokurenko

Reputation: 6132

The on roles block takes an optional argument host, and that host knows what roles are assigned to it.

Example:

task :some_task do
  on roles(:all) do |host|
    puts host.roles.to_a
  end
end

The host is an instance of Capistrano::Configuration::Server, and you can check whether it is related to some specific role like this: host.has_role?(:db).

Upvotes: 3

Related Questions