Ansible lineinfile insertafter duplicate lines

Ansible linefile insertafter duplicate lines

我想修复 VBox 中损坏的发送文件支持,所以我需要在 . 我想用 ansible 剧本来做到这一点。具体任务如下:

- name: fix broken sendfile support in VBox
  lineinfile:
    dest: /etc/apache2/sites-enabled/000-default
    regexp: '^ServerAdmin'
    insertafter: 'ServerAdmin'
    line: 'EnableSendfile off'
    state: present

当我需要再次调用 playbook 时问题越来越严重,而且这个任务重复了一行。如何解决这个问题。

您的任务将在第一个 运行 将 ServerAdmin 替换为 EnableSendfile off,并在随后的 运行 中替换 ServerAdmin(因为没有 ServerAdmin替换)将 EnableSendfile off 添加到底部。 由于 regexppattern to replace if found,您可以尝试将 EnableSendfile off 放在那里:

- name: fix broken sendfile support in VBox
  lineinfile:
    dest: /etc/apache2/sites-enabled/000-default
    regexp: 'EnableSendfile off'
    insertafter: 'ServerAdmin'
    line: 'EnableSendfile off'
    state: present