Action when process stops appending to a file

How do I get Ansible to monitor a log file and take an action when the process stops appending?

I have a high volume process that we’ll drain before stopping it. The process will have queued a significant number of transactions just prior being placed in administrative mode. The process will continue to handle those items while appending to the log. I want Ansible to look at the file, verify that nothing is being added to it, and then stop the service.

How do I express this in a playbook?

I have played arouind a bit and came up with this. Maybe it will help:

  • hosts: localhost
    sudo: true
    tasks:

  • name: Create file
    file: path=/testfile state=touch

  • name: Get mtime
    stat: path=/testfile
    register: testfile

  • name: wait till there have been no changes to the file for 10 seconds
    debug: msg=“Not yet”
    register: result
    until: “{{lookup(‘pipe’, ‘date +%s’)}}-testfile.stat.mtime >=10”
    delay: 3
    retries: 10
    ignore_errors: true

  • debug: var=result

I tried this, but Ansible exits while my test shell script is appending lines to the file.

until: “{{lookup(‘pipe’, ‘date +%s’)}}-testfile.stat.mtime >=10”

Is there some good documentation on ‘until’?

until: result.stdout.find("all systems go") != -1

Where can I find the various methods (such as stdout.find) that are available for Ansible? Is there a good list somewhere? I’ve been looking around and not finding much.

For do until:

https://docs.ansible.com/ansible/playbooks_loops.html#do-until-loops

The output of the stat module is documented in the documentation page of the module. But you can look at what is available in general by registering the output of a task and then putting debug: var=variablename as the next task.

I haven’t put any error handling in my example playbook so you would probably have to add another task in case the loop times out. The values I chose were OK for testing but you will need to find proper timings for your use case.
additionally you can read up on lookups here: https://docs.ansible.com/ansible/playbooks_lookups.html

I chose to not use to look into the stdout of another task because you specifically asked to look if anything still appends to a file. If you have a log entry that tells you when your processes are finished I would recommend using that as an inidcator instead og the mtime.