-1

I am trying to grep the keyword ERROR in all log files present in the directory /home/application/logs using the following playbook:

---
- hosts: localhost
  become: yes
  gather_facts: False

  tasks:

    - name: Find the log files
      find: 
        paths: /home/application/logs 
        pattern: '*Errors.log'
      register: logfiles

    - name: Check for errors in Logfiles
      shell: 
        cmd: "cat {{ item.path }} | grep ERROR"
      with_items: "{{ logfiles.files }}"

This is what I get:

PLAY [localhost] ********************************************************************************************************************************************

TASK [Find the log files] ***********************************************************************************************************************************
ok: [localhost]

TASK [Check for errors in Logfiles] *************************************************************************************************************************
changed: [localhost] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': True, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1642488884.289124, u'gr_name': u'root', u'path': u'/home/application/logs/AgentErrors.log', u'xusr': True, u'atime': 1642850644.2357764, u'inode': 524344, u'isgid': False, u'size': 292, u'isdir': False, u'ctime': 1642832206.1996558, u'isblk': False, u'wgrp': False, u'xgrp': True, u'isuid': False, u'dev': 64771, u'roth': True, u'isreg': True, u'isfifo': False, u'mode': u'0755', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True})
changed: [localhost] => (item={u'rusr': True, u'uid': 0, u'rgrp': True, u'xoth': False, u'islnk': False, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1642832446.9004142, u'gr_name': u'root', u'path': u'/home/application/logs/AppErrors.log', u'xusr': False, u'atime': 1642832446.9034142, u'inode': 524434, u'isgid': False, u'size': 402, u'isdir': False, u'ctime': 1642832446.9004142, u'isblk': False, u'wgrp': False, u'xgrp': False, u'isuid': False, u'dev': 64771, u'roth': True, u'isreg': True, u'isfifo': False, u'mode': u'0644', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True})

PLAY RECAP **************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

All I want is the line inside the logs containing the keyword ERROR.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 3
    As an aside, that's a [useless `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat); `grep` already knows how to examine a collection of files (use `grep -h` if you don't want to see the file names). Actually the `find` seems useless, too; `grep -h ERROR /home/application/logs/*Errors.log` – tripleee Jan 22 '22 at 12:59
  • 2
    Run in verbose mode as a fast workaround. Better: register the output from your grep task in a variable and spit it out with a debug. – Zeitounator Jan 22 '22 at 13:32

1 Answers1

0

In respect to your use case and the comments, a simple solution like

---
- hosts: localhost
  become: yes
  gather_facts: false

  vars:

    MESSAGE: "ERROR"
    LOG_PATH: "/home/application/logs"
    LOG_FILES: "*Errors.log"

  tasks:

    - name: Check for {{ MESSAGE }} in log files
      shell:
        cmd:  "grep -hn {{ MESSAGE }} {{ LOG_PATH }}/{{ LOG_FILES }}"
      register: loglines

    - name: Show result
      debug:
        msg: "{{ loglines.stdout }}"

might work for you.

U880D
  • 8,601
  • 6
  • 24
  • 40