10

I have multi-stage multi-server setup and in my task I need to use server name e.g. in stagin.rb I have:

set :stage, :staging
# Define servers
server 'xxx.xx.xx.xxx', user: 'deploy', roles: %w{app}, name: 'app1'
server 'xxx.xx.xx.yyy', user: 'deploy', roles: %w{app}, name: 'app2'

and I want to use that "name" variable in my task:

task :configure do
  on roles(:app), in: :parallel do
  # how do I get server name here?
  end
end
a.yastreb
  • 1,493
  • 1
  • 10
  • 11

1 Answers1

16

If you want to return the hostname / IP, then it will be

task :configure do
  on roles(:app), in: :parallel do |server|
    p server.hostname # server hostname should be in here
  end
end

If you would like to access custom properties, like :name in this particular case, they are stored in the properties hash of the server configuration object: just use server.properties.name instead of server.hostname.

bredikhin
  • 8,875
  • 3
  • 40
  • 44
  • this actually returns the name of a task :) if I change variable name in server definition to "appname" and try "puts server.appname" I get "Undefined method 'appname' for – a.yastreb Jan 21 '14 at 09:36
  • @a.yastreb Sorry, my bad, misplaced parameter. I corrected the answer, thanks for the heads-up. – bredikhin Jan 21 '14 at 15:33