监控日志

https://stackoverflow.com/questions/53385430/monitor-recent-entries-of-a-log-file-with-ansible

  • 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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容