Naveen Penki
Naveen Penki

Reputation: 35

validating log file before continuing the playbook execution

I want to look for particular sentence '*****--Finished Initialization**' in log before starting the application on next host. The tail command will never stop printing the data since the application will be used by some process immediately after application start and data will be logged.

how can I validate that before restarting the application on the second host? As of now, I have skipped the tail command and given a timeout of 3 minutes.

- 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

Upvotes: 0

Views: 2108

Answers (2)

Baptiste Mille-Mathias
Baptiste Mille-Mathias

Reputation: 2169

You have to use the wait_for module to look for a particular string with regex

- 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 can also be used to detect for file apparition (like pid) or network port being opened (or not).

also, always prefer using native module instead of using a shell script to handle deployment or action. That why I replace the shell with service module.

Upvotes: 0

techraf
techraf

Reputation: 68519

Use 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

Notice if the log is not cleared between app restarts and contains multiple Finished Initialization strings inside, refer to this question.

Upvotes: 3

Related Questions