Read more

How to fix "unknown role" errors in Capistrano recipes

Arne Hartherz
December 02, 2011Software engineer at makandra GmbH

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

`role_list_from': unknown role `something' (ArgumentError)
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2011-12-02 16:38)