Using conditional check for hosts in task definition

141 views
Skip to first unread message

Noman Amir Jamil

unread,
Feb 8, 2012, 4:12:34 AM2/8/12
to capis...@googlegroups.com
Hi All,

I was wondering if it is possible to include a conditional check in task definition. For example, I have a task defined:


task :check_tomcat6_status, :roles => :app do
    sudo "/sbin/service tomcat6 status"
    run "sleep 15s"
end

I want to introduce a conditional hosts check in it, like:

if :hosts = host1 then
   sudo "/sbin/service tomcat5 status"
else
  sudo "/sbin/service tomcat6 status"
fi
 run "sleep 15s"
end

Can someone help me with the correct syntax for this scenario?

Thanks
Noman A.

Lee Hambley

unread,
Feb 8, 2012, 4:24:13 AM2/8/12
to capis...@googlegroups.com
That's not possible, as the task body runs in parallel on all hosts.
--
* You received this message because you are subscribed to the Google Groups "Capistrano" group.
* To post to this group, send email to capis...@googlegroups.com
* To unsubscribe from this group, send email to capistrano+...@googlegroups.com For more options, visit this group at http://groups.google.com/group/capistrano?hl=en

Rafa García

unread,
Feb 8, 2012, 6:10:06 AM2/8/12
to capis...@googlegroups.com
Hi Noman,

  You can do it using shellscript:

task :check_tomcat_status, :roles => :app do
    run "#{sudo} /usr/local/sbin/check_tomcat_status.sh"
    run "sleep 15s"
end

Being check_tomcat_status.sh a shell script that check, i.e., if service tomcat6 exists and do the work else do the work with tomcat5


   Another option could be define the role with tomcat5 and tomcat6 modifier:

role :app, "127.0.0.1", :tomcat5 => true
role :app, "127.0.0.2", :tomcat6 => true

   Later define the task:

task :check_tomcat_status, :roles => :app do
    run "#{sudo} /sbin/service tomcat5 status", :only => {:tomcat5 => true}
    run "#{sudo} /sbin/service tomcat6 status", :only => {:tomcat6 => true}
    run "sleep 15s"
end

Noman Amir Jamil

unread,
Feb 8, 2012, 7:07:42 AM2/8/12
to capis...@googlegroups.com
Hi Garcia,

Thank you so much for the detailed input. I will give both the options a shot and see which one gets my task done!

Regards
Noman A.
Reply all
Reply to author
Forward
0 new messages