使用this - solved - question涉及带有“ wait_for”和“ regex_search”的正则表达式搜索到日志文件中,我询问是否可以对多个正则表达式使用wait_for条件。
我正在成功使用Sami Badra's solution :
vars:
log_file: <path_to_log_file>
pattern_to_match: <pattern_to_match>
tasks:
- name: "Get contents of log file: {{ log_file }}"
command: "cat {{ log_file }}"
changed_when: false
register: cat_output
- name: "Create variable to store line count (for clarity)"
set_fact:
line_count: "{{ cat_output.stdout_lines | length }}"
##### DO SOME OTHER TASKS (LIKE DEPLOYING APP) #####
- name: "Wait until '{{ pattern_to_match}}' is found inside log file: {{ log_file }}"
wait_for:
path: "{{ log_file }}"
search_regex: "^{{ pattern_to_skip_preexisting_lines }}{{ pattern_to_match }}$"
state: present
vars:
pattern_to_skip_preexisting_lines : "(.*\\n){% raw %}{{% endraw %}{{ line_count }},{% raw %}}{% endraw %}" # i.e. if line_count=100, then this would equal "(.*\\n){100,}
匹配单个正则表达式(例如“ ABCD System Started OK”)。
现在我正在尝试使用相同的wait_for来捕获多个正则表达式,例如
,然后使用fail / debug继续或停止...但是我在使用正则表达式或with_items数组的每次测试中都失败了。
是否可以在Ansible上同时检查多个正则表达式?
如果是,有更好的方法或优雅的解决方案吗?
答案 0 :(得分:0)
使用ansible 2.2.1.0和2.6.1对我有用:
- name: wait for multiple regex
become: yes
wait_for:
path: "{{ log_file }} "
search_regex: "{{ item }}"
with_items:
- "ABCD System Started OK"
- "ABCD System Failed"