【问题标题】:Modify delete a line using ansible使用ansible修改删除一行
【发布时间】:2019-12-02 21:49:15
【问题描述】:

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

foo bar foo.bar=hold fsck.mode=auto foo foo bar foo

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

如果可能的话,我更喜欢使用 lineinfile 而不是替换。我不想删除整行,而只想删除带有 '' 的正则表达式中的内容。

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

【问题讨论】:

    标签: ansible


    【解决方案1】:
    - 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: ''
    

    以上内容对我来说可以将“']”替换为''

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

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

    【讨论】:

      【解决方案2】:

      我认为您对反向引用感兴趣。下面的代码可以满足您的需要。如果你愿意,你也可以玩空格

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

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

      【讨论】:

      • 末尾的$有什么意义?在 SED 中是行尾
      • @damaged1988 这里也一样
      猜你喜欢
      • 2022-06-19
      • 1970-01-01
      • 2021-06-03
      • 2018-03-23
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多