ansible - 在继续执行剧本之前验证日志文件
问题描述
我想在下一个主机上启动应用程序之前在日志中查找特定的句子“*****--Finished Initialization**”。tail 命令将永远不会停止打印数据,因为应用程序将在应用程序启动后立即被某个进程使用,并且将记录数据。
如何在第二台主机上重新启动应用程序之前验证这一点?到目前为止,我已经跳过了 tail 命令并给出了 3 分钟的超时时间。
- name: playbook to restart the application on hosts
hosts: host1, host2
tags: ddapp
connection: ssh
gather_facts: no
tasks:
- name: start app and validate before proceeding
shell: |
sudo systemctl start tomcat@application
#tail –f application_log.txt
wait_for: timeout=180
#other shell commands
args:
chdir: /path/to/files/directory
解决方案
使用wait_for
模块:
- name: playbook to restart the application on hosts
hosts: host1, host2
tags: ddapp
connection: ssh
gather_facts: no
tasks:
- name: start app
become: yes
service:
name: tomcat@application
state: started
- name: validate before proceeding
wait_for:
path: /path/to/files/directory/application_log.txt
search_regex: Finished Initialization
请注意,如果在应用程序重新启动之间日志未清除并且Finished Initialization
内部包含多个字符串,请参阅此问题。
推荐阅读
- http - 基于 https 服务器 python 在提供响应后关闭连接
- mysql - django - 记录未保存在 MySQL 数据库中,但 AutoIncrement 增加
- c# - C# WPF 从 ListBox.selectedItem 接收的对象能够修改 itemSource 集合,如何?
- python - 在 Pandas 中计算每月的最后一个星期五
- c# - 命令优先于子模块组 Discord.Net
- sql-server - SQL Server:如何检测更新表/索引统计信息的适当时机
- nginx - 只允许一台主机使用入口 nginx configmap 进行连接
- c# - 动态获取一个 DbSet
按实体类名 - wordpress - 计算给定小部件的活动实例
- python - Python web-scraping 转到下一页