Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible wait_for module, start at end of file

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

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.

like image 377
user2176100 Avatar asked Dec 18 '16 00:12

user2176100


People also ask

What is Wait_for in Ansible?

wait_for - Waits for a condition before continuing. — Ansible Documentation. You are reading an unmaintained version of the Ansible documentation. Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE).

How do you add delays in Ansible?

To pause/wait/sleep per host, use the ansible. builtin. wait_for module. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely.

What is the default module in Ansible?

Module defaults groupsAnsible 2.7 adds a preview-status feature to group together modules that share common sets of parameters. This makes it easier to author playbooks making heavy use of API-based modules such as cloud modules. The docker_stack module is not included in the docker defaults group.

How does an Ansible module work?

Efficient architecture For automating Linux and Windows, Ansible works by connecting to managed nodes and pushing out small programs, called "Ansible modules," to them. These programs are written to be resource models of the desired state of the system.


1 Answers

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 }}"
like image 99
techraf Avatar answered Sep 27 '22 00:09

techraf