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

查看:745
本文介绍了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天全站免登陆