Ansible wait_for 模块,从文件末尾开始 [英] Ansible wait_for module, start at end of file

查看:34
本文介绍了Ansible wait_for 模块,从文件末尾开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在文件上使用 search_regex='foo',则使用 Ansible 中的 wait_for 模块

With the wait_formodule in Ansible if I use search_regex='foo' on a file

它似乎从文件的开头开始,这意味着它将匹配旧数据,因此当重新启动附加到文件而不是启动新文件的进程/应用程序(Java)时,wait_for 模块将退出 true 旧数据,但我想从文件的尾部检查.

it seems to start at the beginning of the file, which means it will match on old data, thus when restarting a process/app (Java) which appends to a file rather than start a new file, the wait_for module will exit true for old data, but I would like to check from the tail of the file.

推荐答案

wait_for 模块的 search_regex 中的正则表达式默认设置为多行.

Regular expression in search_regex of wait_for module is by default set to multiline.

您可以注册最后一行的内容,然后搜索出现在该行之后的字符串(假设日志文件中没有重复的行,即每行都包含一个时间戳):

You can register the contents of the last line and then search for the string appearing after that line (this assumes there are no duplicate lines in the log file, i.e. each one contains a time stamp):

vars:
  log_file_to_check: <path_to_log_file>
  wanted_pattern: <pattern_to_match>

tasks:
  - name: Get the contents of the last line in {{ log_file_to_check }}
    shell: tail -n 1 {{ log_file_to_check }}
    register: tail_output

  - name: Create a variable with a meaningful name, just for clarity
    set_fact:
      last_line_of_the_log_file: "{{ tail_output.stdout }}"

  ### do some other tasks ###

  - name: Match "{{ wanted_pattern }}" appearing after "{{ last_line_of_the_log_file }}" in {{ log_file_to_check }}
    wait_for:
      path: "{{ log_file_to_check }}"
      search_regex: "{{ last_line_of_the_log_file }}\r(.*\r)*.*{{ wanted_pattern }}"

这篇关于Ansible wait_for 模块,从文件末尾开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆