Sonntag, 19. Juni 2011

replace template information inside deployed files when using capistrano

When you for example need some host specific information like a IP address in some files when you deploy something to multiple hosts via capistrano then you can use something like following:

This will output the IP-Address of interface eth1 from each host

desc "Print local ip address"
task :show_local_ip, :roles => [:app] do
  run "ifconfig eth1  | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'"
end
You can extend it like this to replace a given template text inside a file with the IP from one interface

desc "replace local ip address"
task :replace_local_ip, :roles => [:app] do
  run "ifconfig eth1  | grep 'inet addr:'| cut -d: -f2 | awk '{ printf $1}' | xargs -0 -INEWIP perl -pi.bak -e 's/%%IPADDRESS%%/NEWIP/' #{current_releasse}/myconfig"
end
This  will replace %%IPADDRESS%% inside the myconfig file with the IP-Address of your Interface eth1

Keine Kommentare:

Kommentar veröffentlichen