首页 > 解决方案 > 在继续执行剧本之前验证日志文件

问题描述

我想在下一个主机上启动应用程序之前在日志中查找特定的句子“*****--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

标签: ansible

解决方案


使用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内部包含多个字符串,请参阅此问题


推荐阅读