lineinfile creates a backup everytime it is used in a role

Hello all,

I have to make sure that certain lines are present in the file /etc/postfix/main.cf. I’m not allowed to control the whole file, so I cannot use the template module. The lines are only allowed to exist once in this file, so I cannot use blockinfile, because the same line could exist elsewhere in the file. So I stick with lineinfile and use it like in this example:

`

roles/postfix/tasks/main.yml

One solution.

Before the first lineinfile save the file.

- slurp:
     src: /etc/postfix/main.cf
   register: _slurp_main_cf

Remove the backup: yes in all lineinfile and add a second notify on all
   - store original

In the handlers add

- name: store original
   copy:
     content: '{{ _slurp_main_cf.content | b64decode }}'
     dest: /path/to/backup/main.cf-{{ lookup('pipe', 'date +%s') }}

That works for me.

Thanks a lot.

Maybe set backup: yes on the first lineinfile task and backup: no on the 2nd and third lineinfile so you only have one backup each time you run the playbook instead of three?

Problem with that solution is that if the first on is present, but the 2nd and/or the 3rd is not, you will not get a backup file.