Carlos Zambrano
Carlos Zambrano

Reputation: 11

Monitor recent entries of a log file with Ansible

I hope you guys can help me.

Basically I need a piece of Ansible code where I send a HUP signal to a process in Solaris, then I need to monitor a log file (linked to that process) that gets populated as the HUP signal is processed. The idea is that a task gets completed when the string "Configuration data reloaded" is found on that log file (otherwise timeout and fail). Note that string is present in the log from previous events, but I need to monitor the more recent entries that are being sent there.

This is the section that I've tried so far, with no luck (even though the string appears after a couple of minutes in the logfile). Note the "tail" below only shows the latest line at that moment. I tried with "-f" but the task kind of hangs there, not going nowhere.

- name: HUP certain_process
  command: "pkill -HUP certain_process"

- name: Get the content of the last line in logfile
  shell: tail -1 logfile
  register: tail_output

- name: Waits until process HUP finishes
  wait_for:
    path: '{{tail_output.stdout_lines}}'
    search_regex: "Configuration data reloaded"
    delay: 20
    timeout: 280
  register: wait
  ignore_errors: True

- name: Fail if HUP failed
  fail: msg="App HUP failed"
  when: wait|failed

and the output:

TASK [HUP certain_process] **************************************************************
Tuesday 20 November 2018  01:14:18 +0000 (0:00:05.575)       0:00:20.148 ******
changed: [host1]

TASK [Get the contents of the last line in logfile] ***************
Tuesday 20 November 2018  01:14:23 +0000 (0:00:05.413)       0:00:25.561 ******
changed: [host1]

TASK [Waits until process HUP finishes] ****************************************
Tuesday 20 November 2018  01:14:27 +0000 (0:00:04.067)       0:00:29.629 ******
fatal: [host1]: FAILED! => {"changed": false, "elapsed": 280, "failed": true, "msg": "Timeout when waiting for search string Configuration data reloaded in ['Nov 20 01:44:28 host1 SYS(15) HUP signal detected: reloading configuration data...']"}
...ignoring

TASK [Fail if hupped failed] ***************************************************
Tuesday 20 November 2018  01:19:13 +0000 (0:04:45.737)       0:05:15.366 ******
fatal: [host1]: FAILED! => {"changed": false, "failed": true, "msg": "App HUP failed"}
        to retry, use: --limit @/export/home/carlos/project/policyUpdate.retry

Upvotes: 1

Views: 2369

Answers (1)

mdaniel
mdaniel

Reputation: 33168

If nothing else, this is incorrect:

- name: Waits until process HUP finishes
  wait_for:
    path: '{{tail_output.stdout_lines}}'
    search_regex: "Configuration data reloaded"

as the fine manual tells you, the path: field is expecting a path on the filesystem, and not a list of str (which is what is contained within stdout_lines).

I actually think you want until: with possibly until: "Configuration data reloaded" in tail_output.stdout (since you don't need the regex feature for a static string). Although I said possibly because realistically using tail -1 with a delay: means you run the very real risk of the "magic string" appearing in the file but being missed because it has appeared during the delay: window but was pushed aside by a subsequent line.

I think you may be much happier with tail -n 20 or something like it, to give the delay: in combination with the verbosity of the log file content time to overlap.

Upvotes: 1

Related Questions