mikita agrawal
mikita agrawal

Reputation: 701

How to check if my tomcat has started using Ansible

I am writing a ansible script to automate my application deployment. I use svcadm commands to check tomcat status

enable tomcat - svcadm enable tomcat disable tomcat - svcadm disable tomcat check tomcat status - svcs tomcat

When i run the enable command after the deployment, tomcat immediatly goes into online mode and that's how I am checking if my tomcat is UP like this -

 - name: Verifying tomcat service to make sure it is in online state
   shell: "svcs tomcat"
   register: result
   until: result.stdout_lines.1.split().0 == "online"
   retries: 5
   delay: 30

BUT the thing is that does not necessary means the website is accessible. We verify that from the below logs -

09-Oct-2020 13:01:39.823 INFO [localhost-startStop-2] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /app/apache-tomcat-8.0.9/conf/Catalina/localhost/selfcare-enhanced-userinfo-mock.xml has finished in 31,590 ms
09-Oct-2020 13:01:41.003 INFO [localhost-startStop-4] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /app/apache-tomcat-8.0.9/conf/Catalina/localhost/myapp1.xml has finished in 89,863 ms
09-Oct-2020 13:01:51.960 INFO [localhost-startStop-3] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /app/apache-tomcat-8.0.9/conf/Catalina/localhost/myapp2.xml has finished in 52,709 ms
09-Oct-2020 13:01:51.994 INFO [localhost-startStop-5] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /app/apache-tomcat-8.0.9/webapps/psdv1
09-Oct-2020 13:02:36.948 INFO [localhost-startStop-5] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
09-Oct-2020 13:02:36.963 INFO [localhost-startStop-5] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /app/apache-tomcat-8.0.9/webapps/psdv1 has finished in 44,969 ms
09-Oct-2020 13:02:36.969 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
09-Oct-2020 13:02:36.993 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
**09-Oct-2020 13:02:36.997 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 308347 ms**

Is there a way to check/monitor this log file and check for that last line and wait until this log occurs which tells us that the tomcat is up and running and website is accessible now and we terminate the script?

Upvotes: 0

Views: 2506

Answers (1)

seshadri_c
seshadri_c

Reputation: 7340

Option 1:

You can use the Ansible module wait_for for this purpose, and look for the term Server startup in the log file.

Example path of tomcat logs /opt/tomcat/logs/catalina.out:

# Empty contents of catalina.out before this task with some method

  - wait_for:
      path: /opt/tomcat/logs/catalina.out
      search_regex: "Server startup"
      # set timeout if it usually takes more than 300 seconds to start
      timeout: 600

Note that this requires the file to be empty to avoid matching an older Server startup.

Option 2:

You can tail the logs and look for Server startup until it is found:

# Pause few seconds for logs to roll with initialization of Tomcat
  - pause:
      seconds: 30

# Tail the log 15 times with 20 second interval looking for Server startup (300 secs)
  - command: tail /opt/tomcat/logs/catalina.out
    register: catalina_tail
    until: catalina_tail.stdout is search('Server startup')
    retries: 15
    delay: 20

You can set the retries and delay according to the time it usually takes for Tomcat to completely start.

Though in my experience with Tomcat, I haven't felt the need to preserve the catalina.out logs. So I would prefer Option 1.

Upvotes: 2

Related Questions