Parsing log files with timestamp Ansible

I would like to search/wait for a string in a recently created file(test_build_201708211712.log) which has timestamp.

-rw-rw-r-- 1 test test 579 Aug 21 16:00 test_build_201708211559.log
-rw-rw-r-- 1 test test 579 Aug 21 17:12 test_build_201708211712.log

How can I achieve this in Ansible?

you can use lineinfile module for this.

for more understanding on lineinfile module you can go through to the link “http://docs.ansible.com/ansible/latest/lineinfile_module.html”

if anything specific is required please elaborate in details.

I want to find the recently created file under a folder and cat to the file for the contents updates which can be visible while running a play in output.

I want to find the recently created file under a folder and cat to the file for the contents updates which can be visible while running a play in output.

use this play in your playbook you will get expected result:

  • name: safely use
    shell: find /var/log/ -maxdepth 1 -type f -printf ‘%TY-%Tm-%Td %TT %p\n’| sort -r | head -1 | awk ‘{print $3}’ | xargs cat
    register: myoutput
  • debug: var=myoutput.stdout_lines

here </var/log/> → give your folder path from where you want to cat the file for the contents.