我的正则表达式可以获取正确的单词;然而,ansible 无法改变它 enter image description here
我的ansible代码:
hosts: all
gather_facts: False
become: true
tasks:
- name: Checking if chargen configuration is present in the files
ansible.builtin.replace:
path: /etc/xinetd.d/chargen
regexp: '(?<=disable\s{9}\S\s)yes'
replace: 'no'
register: test
- name: Gathered
ansible.builtin.debug:
msg: "{{ test }}"
结果:“ok”但没有改变
最佳答案
你可以使用
tasks:
- name: Checking if chargen configuration is present in the files
ansible.builtin.replace:
path: /etc/xinetd.d/chargen
regexp: '^(\s*disable\s*=\s*)yes\s*$'
line: '\g<1>no'
register: test
- name: Gathered
ansible.builtin.debug:
msg: "{{ test }}"
这里,
-
^(\s*disable\s*=\s*)yes\s*$
- 火柴-
^
- 字符串开始 -
(\s*disable\s*=\s*)
- 捕获组 1(可以用\1
或\g<1>
表示):零个或多个空格,disable
, 零个或多个空格,=
, 零个或多个空格 -
yes
-yes
字符串 -
\s*
- 零个或多个空格 -
$
- 字符串结束。
-
-
'\g<1>no'
用第 1 组和no
中的值替换匹配的行字符串。
关于regex - Ansible 正则表达式环顾四周无法替换模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69280029/