mipmip
mipmip

Reputation: 1160

Capistrano 3 multiple server lazy variables overriding

I've problems implementing a config in Capistrano v3 to multiple servers in one stage. Or I want something that is not possible.

I've created a test task in deploy.rb shown below:

namespace :test do
  task :run do
    on roles(:all) do
      print "\nenvcode:\n"
      p fetch(:envcode)
      print "\nglobal_var:\n"
      p fetch(:global_var)
      print "\nssh_options:\n"
      p fetch(:ssh_options)
      print "\nmain_domain_name:\n"
      p fetch(:main_domain_name)
    end
  end
end

And here's a config which defines to simular servers in the same stage:

role :app, %w{ip1 ip2}
role :web, %w{ip1 ip2}
role :db,  %w{ip1 ip2}

set :main_domain_name, "#{fetch(:envcode)}.mytestdomain.com"
set :global_var, 'foobar'

set :ssh_options, -> do
  { 
    user: 'userip0'
  }
end

server 'ip1',
  roles: %w{web db app},
  ssh_options: { user: 'userip1' },
  user: 'userip1',
  envcode: "envip1",
  dbname: 'userip1',
  dbuser: 'userip1',
  dbpass: 'password',
  dbhost: 'localhost'

server 'ip2',
  roles: %w{web db app},
  ssh_options: { user: 'userip2' },
  user: 'userip2',
  envcode: "envip2",
  dbname: 'userip2',
  dbuser: 'userip2',
  dbpass: 'password',
  dbhost: 'localhost'

When I run cap staging test:run I get this output meaning that capistrano totally ignores my server overridings:

envcode:
nil

global_var:
"foobar"

ssh_options:
{:user=>"userip0"}
envcode:
nil

main_domain_name:
".mytestdomain.com"

global_var:
"foobar"

ssh_options:
{:user=>"userip0"}

main_domain_name:
".mytestdomain.com"

I was hoping to get:

envcode:
envip1

global_var:
"foobar"

ssh_options:
{:user=>"userip1"}
envcode:
envip2

main_domain_name:
"envip1.mytestdomain.com"

global_var:
"foobar"

ssh_options:
{:user=>"userip2"}

main_domain_name:
"envip2.mytestdomain.com"

Am I doing something wrong or is my understanding about what I can do with these server config arrays wrong?

Upvotes: 0

Views: 654

Answers (1)

scorix
scorix

Reputation: 2516

envcode is a property of the server, you can't just use fetch method to get the value.

namespace :test do
  task :run do
    on roles(:all) do |server|
      puts "envcode: #{server.properties.fetch(:envcode)}\n"
      puts "global_var: #{fetch(:global_var)}\n"
      puts "ssh_options: #{server.properties.fetch(:ssh_options)}\n"
      puts "main_domain_name: #{server.properties.fetch(:envcode)}.mytestdomain.com\n"
    end
  end
end

Upvotes: 0

Related Questions