Search code examples
ansibleidempotent

Ansible linefile insertafter duplicate lines


I want to fix broken sendfile support in VBox, so I need to put line in . I want to do that with ansible playbook. Specific task look like:

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

Problem is hapening when I need to call playbook again, and this task duplicate line . How to fix that.


Solution

  • Your task will on the first run replace ServerAdmin with EnableSendfile off and on subsequent runs it will (since there is no ServerAdmin to replace) add EnableSendfile off to the bottom. Since regexp is pattern to replace if found, you can try putting EnableSendfile off there:

    - 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