How to fix "unknown role" errors in Capistrano recipes

Updated . Posted . Visible to the public.

When you have a complex recipe setup with multistage deployment you may run into this error:

`role_list_from': unknown role `something' (ArgumentError)

Consider this task definition:

namespace :foo do
  task :bar, :roles => :something do
    # do crazy stuff
  end
end

Whenever we call foo.bar in our recipe, Capistrano will fail if you deploy to a stage where none of the servers has the role the error complains about, "something" in this case.

However, you can hack around it Show archive.org snapshot :

  1. Put a roles[:something] into your deploy.rb to avoid the above error message.\
    Now, when Capistrano enters a hook and tries to call a method that is only available for that group of servers, you'll get another error:

    `foo.bar' is only run for servers matching {:roles=>:something}, but no servers matched
    
  2. To properly fix it, you also need to avoid running any commands inside that task if no server matches the task's role:

    roles[:something] # To make Capistrano aware of it
    # ...
    namespace :foo do
      task :bar, :roles => :something do
        next if find_servers_for_task(current_task).empty? # Skip for certain setups
        # do crazy stuff
      end
    end
    

As always, remember to properly hook.

Arne Hartherz
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-12-02 15:38)