regex - Ansible 正则表达式环顾四周无法替换模式

标签 regex ansible

我的正则表达式可以获取正确的单词;然而,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”但没有改变

enter image description here enter image description here

最佳答案

你可以使用

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/

相关文章:

elasticsearch - Ansible 剧本出错了

python-3.x - 升级 ansible 以在 Controller 上使用 python3

java - 使用 contains() java 读取大文件并进行过滤

java - 在Java中使用正则表达式删除html标签

ansible - 如何在ansible中仅发送文件路径?

solr - 在 Ansible 中从主机构建动态命令

ansible - 通过 Ansible 查找包含程序的所有 CICS 区域

c# - 用于替换除数字之外的所有字符的正则表达式

python - Django URL 正则表达式 "is not a valid regular expression"错误

regex - 向数字添加逗号时需要环视解释