繁体   English   中英

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

[英]validating log file before continuing the playbook execution

在下一个主机上启动应用程序之前,我想在日志中查找特定句子“ *****-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字符串,请参阅此问题

您必须使用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脚本来处理部署或操作。 这就是为什么我用service模块替换shell原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM