我正在寻找一种使用wait_for测试空文件的方法,但是我似乎找不到有效的正则表达式。理论上,“ ^ $”应该可以工作,但实际上并非如此。
示例剧本(可使用2.7.1):
- name: Create file with data
lineinfile:
dest: /tmp/test_file
line: 12345
state: present
create: yes
- name: Wait for /tmp/test_file to contain 12345 - this test should pass
wait_for:
path: /tmp/test_file
search_regex: "12345"
timeout: 2
- name: Wait for /tmp/test_file to be empty - this test should fail but it passes
wait_for:
path: /tmp/test_file
search_regex: "^$"
timeout: 2
答案 0 :(得分:0)
我敢打赌正确的正则表达式为\A\Z
:
- name: Wait for /tmp/test_file to be empty - this test should fail but it passes
wait_for:
path: /tmp/test_file
search_regex: '\A\Z'
timeout: 2
$
和^
使用MULTILINE正则表达式很棘手。
请参见https://docs.python.org/3/library/re.html#regular-expression-syntax
\A
仅在字符串的开头匹配。
\Z
仅在字符串末尾匹配。