如何检查我的tomcat是否已开始使用Ansible

如何检查我的tomcat是否已开始使用Ansible,tomcat,ansible,Tomcat,Ansible,我正在编写一个ansible脚本来自动化我的应用程序部署。我使用svcadm命令检查tomcat状态 启用tomcat-svcadm启用tomcat 禁用tomcat-svcadm禁用tomcat 检查tomcat状态-svcs tomcat 当我在部署后运行enable命令时,tomcat立即进入联机模式,这就是我检查tomcat是否像这样启动的方式- - name: Verifying tomcat service to make sure it is in online state

我正在编写一个ansible脚本来自动化我的应用程序部署。我使用
svcadm
命令检查tomcat状态

启用tomcat-
svcadm启用tomcat
禁用tomcat-
svcadm禁用tomcat
检查tomcat状态-
svcs tomcat

当我在部署后运行enable命令时,tomcat立即进入
联机
模式,这就是我检查tomcat是否像这样启动的方式-

 - 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
但问题是,这并不意味着网站是可访问的。我们从以下日志中验证-

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**

是否有办法检查/监视此日志文件,检查最后一行,并等待此日志出现,该日志告诉我们tomcat已启动并运行,现在可以访问网站,我们将终止脚本?

选项1:

为此,您可以使用
wait_for
,并在日志文件中查找术语
服务器启动

tomcat日志的示例路径
/opt/tomcat/logs/catalina.out

#使用某种方法在此任务之前清空catalina.out的内容
-等待:
路径:/opt/tomcat/logs/catalina.out
搜索正则表达式:“服务器启动”
#如果启动通常需要300秒以上,则设置超时
超时:600
请注意,这要求文件为空,以避免与较旧的
服务器启动相匹配

选项2:

您可以
跟踪
日志并查找
服务器启动
,直到找到:

#暂停几秒钟,让日志随着Tomcat的初始化而滚动
-暂停:
秒:30
#跟踪日志15次,间隔20秒,查找服务器启动(300秒)
-命令:tail/opt/tomcat/logs/catalina.out
注册号码:catalina_tail
直到:catalina_tail.stdout是搜索('服务器启动')
重试次数:15次
延误:20
您可以根据Tomcat完全启动通常需要的时间设置
重试
延迟


尽管根据我使用Tomcat的经验,我觉得没有必要保存
catalina.out
日志。因此,我更喜欢选项1

检查服务所服务的端口上是否有可用的服务,这不是比解析日志文件和查找启动状态更好更干净吗?@seshadri_c Hi,我尝试过使用
wait\u for
,但仅仅因为端口打开并不意味着tomcat应用程序实际上已经准备好为请求提供服务。所以,这就是为什么我试图查找日志,这样我就知道我的tomcat已经准备好为requestsOk提供服务了,同样的模块也可以用来检查文件。我将发布解决方案。@seshadri\u c好的,我将尝试下面的解决方案。因此,如果我将超时时间设置为5分钟,它是否会完成这5分钟,或者一旦日志行出现在日志文件中,它就会存在?@seshadri_c非常感谢您的详细回复。我使用选项2跟踪Catalina日志,使其工作正常。我将查看是否可以在部署开始前刷新日志,并使用选项1。谢谢你。