使用ansible修改或删除行

使用ansible修改或删除行,ansible,Ansible,我只想使用ansible从以下行中删除'fsck.mode=auto' foo-bar foo.bar=保持fsck.mode=自动foo-foo-bar foo 主机、根用户和其他权限在项目中的其他位置设置 如果可能的话,我更喜欢使用lineinfle而不是replace。我不想删除整行,只想删除regexp中带有“”的内容 以上内容适用于我将“]”替换为“.” 编辑:刚刚注意到您不想使用“替换”我的坏消息。我相信您可以使用“state=present”来替换使用lineinfle 请注意“b

我只想使用ansible从以下行中删除'fsck.mode=auto'

foo-bar foo.bar=保持fsck.mode=自动foo-foo-bar foo

主机、根用户和其他权限在项目中的其他位置设置

如果可能的话,我更喜欢使用lineinfle而不是replace。我不想删除整行,只想删除regexp中带有“”的内容

以上内容适用于我将“]”替换为“.”

编辑:刚刚注意到您不想使用“替换”我的坏消息。我相信您可以使用“state=present”来替换使用lineinfle


请注意“backrefs”选项

我认为您对backrefs感兴趣。下面的代码满足您的需要。如果需要,也可以使用空格


-名称:仅更改匹配
线条填充:
路径:/whatever/file.txt
regexp:“^(.*)fsck.mode=auto(.*)”
行:'\1\2'
背景参考:是的


没有这些间隔。和*(s>>t的格式部分)

结尾处的$的意义是什么?在SED中,这是为了比赛的结束line@damaged1988这里也一样
- name: remove fsck
      lineinfile:
        dest: /etc/grub2.cfg 
        regexp: 'fsck.mode=auto'
        line: ''
- name: Replacing the square brackets and single quotes on new VM IP address in hosts
  hosts: 127.0.0.1
  connection: local
  become: true
  tasks:
    - name: Replacing the brackets from the end of the new ip with whitespace
      replace:
        path: /etc/ansible/hosts
        regexp: \'\]
        replace: ''