Ansible-wait_for空文件

时间:2019-02-22 15:35:59

标签: ansible

我正在寻找一种使用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

1 个答案:

答案 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仅在字符串末尾匹配。