修改使用ansible删除一行

时间:2019-11-26 15:19:50

标签: ansible

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

  

foo bar foo.bar =按住fsck.mode = auto foo foo foo bar

主机,root用户和其他特权在项目中的其他位置设置。

如果可能,我宁愿使用lineinfile而不是replace。我不想删除整行,而只是用''删除正则表达式中的内容。

- name: remove fsck
      lineinfile:
        dest: /etc/grub2.cfg 
        regexp: 'fsck.mode=auto'
        line: ''

2 个答案:

答案 0 :(得分:0)

- 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: ''

以上内容对我来说可以用“

”替换“']”

edit:刚注意到您不想用“替换”我的坏东西。我相信您可以使用'state = present'替换为lineinfile

https://docs.ansible.com/ansible/2.4/lineinfile_module.html注意“ backrefs”选项

答案 1 :(得分:0)

我认为您对反向引用感兴趣。下面的代码可以满足您的需求。您还可以根据需要在空白处玩耍

- name: Change match only lineinfile: path: /whatever/file.txt regexp: '^(. *)fsck.mode=auto (. *)$' line: '\1\2' backrefs: yes

之间没有空格。和*(格式化s >> t)