在继续执行剧本之前先验证日志文件

时间:2018-08-08 17:56:34

标签: 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

2 个答案:

答案 0 :(得分:3)

使用wait_for module

- 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字符串,请参考this question

答案 1 :(得分:0)

您必须使用wait_for模块通过正则表达式查找特定的字符串

- name: start app
  service:
    state: started
    name: tomcat@application
  become: true

- name: Wait for application to be ready
  wait_for:
    search_regex: '\*\*\*\*\*--Finished Initialization\*\*'
    path: /you/path/to/application_log.txt

wait_for也可用于检测文件外观(例如pid)或网络端口是否打开(或不打开)。

此外,总是更喜欢使用本机模块,而不是使用Shell脚本来处理部署或操作。因此,我将shell替换为service模块。

相关问题