# lineinfile creates a backup everytime it is used in a role

**URL:** <https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077>\
**Category:** Ansible Project\
**Created:** [July 18, 2018, 9:47am UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077 "2018-07-18T09:47:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [July 18, 2018, 9:47am UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077/1 "2018-07-18T09:47:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 18, 2018, 12:21pm UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077/2 "2018-07-18T12:21:46Z")

</div>

One solution.

Before the first lineinfile save the file.

- slurp:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /etc/postfix/main.cf  
&nbsp;&nbsp;&nbsp;register: \_slurp\_main\_cf

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

In the handlers add

- name: store original  
&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: '{{ \_slurp\_main\_cf.content | b64decode }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /path/to/backup/main.cf-{{ lookup('pipe', 'date +%s') }}

---

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [July 18, 2018, 1:09pm UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077/3 "2018-07-18T13:09:59Z")

</div>

That works for me.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [July 25, 2018, 5:15am UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077/4 "2018-07-25T05:15:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 25, 2018, 5:50am UTC](https://forum.ansible.com/t/lineinfile-creates-a-backup-everytime-it-is-used-in-a-role/28077/5 "2018-07-25T05:50:22Z")

</div>

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.
