Action when process stops appending to a file

20 views
Skip to first unread message

tom rkba

unread,
Nov 20, 2015, 2:01:53 PM11/20/15
to Ansible Project
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?

Markus Ellers

unread,
Nov 21, 2015, 4:32:47 PM11/21/15
to Ansible Project
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

tom rkba

unread,
Nov 23, 2015, 10:13:55 AM11/23/15
to Ansible Project
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.

Markus Ellers

unread,
Nov 23, 2015, 2:37:42 PM11/23/15
to Ansible Project
For do until:


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.
Reply all
Reply to author
Forward
0 new messages