Ansible lineinfile:哪个匹配项被替换?

问题描述 投票:0回答:1

我正在努力使用 Ansible lineinfile 模块。根据文档,它应该替换输入文件中的第一个匹配项。但当我尝试各种选项时,在我的输入中,我总是会替换“最后一场比赛”。 这是示例代码:

# testing backrefs: option --- - name: testing backrefs option hosts: localhost vars: text: | the first match this won't fit the second match no fit here the third match no fit again file: /tmp/regextext.txt tasks: - name: create test file for lineinfile copy: content: '{{ text }}' dest: "{{ file }}" - name: testing backrefs with lineinfile module lineinfile: regexp: '^(the \w+ match)$' line: 'THIS IS \1' path: "{{ file }}" backrefs: yes - debug: msg: "To show result, run cat {{ file }} "

据我了解模块文档,这应该将输入文本的第一行替换为 
the first match

THIS IS the first match
但我的经验是,在这种情况下,

last

匹配会被替换,如 the third match

THIS IS the third match
输出文件看起来像这样:

the first match this won't fit the second match no fit here THIS IS the third match no fit again

我的 Ansible 是反叛者,还是这只是用户错误?

ansible
1个回答
0
投票

如果正则表达式匹配,最后匹配的行将被替换。

还有

如果需要第一个匹配,请使用(firstmatch=yes)。

这按预期工作:

- name: testing backrefs with lineinfile module lineinfile: regexp: '^(the \w+ match)$' line: 'THIS IS \1' path: "{{ file }}" backrefs: yes firstmatch: yes

	
© www.soinside.com 2019 - 2024. All rights reserved.