# Ansible - overwriting file lineinfile when run in parallel

**URL:** <https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645>\
**Category:** Ansible Project\
**Created:** [June 25, 2019, 10:25am UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645 "2019-06-25T10:25:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josephsimon\_Arokiara](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Josephsimon\_Arokiara](https://forum.ansible.com/u/Josephsimon_Arokiara)\
**Post date:** [June 25, 2019, 10:25am UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645/1 "2019-06-25T10:25:31Z")

</div>

Hi All,

I am running the below code in a playbook which iterates over many hosts. This script writes the value of ver.stdout to a file. I am seeing issue when I run in in parallel. It is overwriting. It works good when serial:1 but it consumes long time since we have large number of hosts.

Any help to run in parallel and improve the time consumption?

- name: Write version output  
local\_action: lineinfile create=yes line={{ item }} insertafter=EOF dest={{ dest\_file }}  
when: ver.stdout != “”  
with\_items:
- “{{ ver.stdout | regex\_replace(‘\r\n’ , ‘#’ )}},{{ inventory\_hostname }}”

Cheers,  
Joseph

---

<div class="post-metadata">

**Author:** ![Josephsimon\_Arokiara](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Josephsimon\_Arokiara](https://forum.ansible.com/u/Josephsimon_Arokiara)\
**Post date:** [June 26, 2019, 11:00am UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645/2 "2019-06-26T11:00:34Z")

</div>



---

<div class="post-metadata">

**Author:** ![Josephsimon\_Arokiara](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Josephsimon\_Arokiara](https://forum.ansible.com/u/Josephsimon_Arokiara)\
**Post date:** [June 26, 2019, 11:01am UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645/3 "2019-06-26T11:01:09Z")

</div>

Hi All,

Any update on this please?

Cheers,  
Simon J

---

<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:** [June 26, 2019, 6:28pm UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645/4 "2019-06-26T18:28:56Z")

</div>

Since you are using local\_action, it will execute on the ansible controller instead of remote host

`local_action` is an alternative way of doing `delegate_to: localhost`. These actions will be executed on local machine (Ansible control host)  
I don’t see how it would behave differently if you use serial:1 or 5 which is the default.

---

<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:** [June 27, 2019, 6:01am UTC](https://forum.ansible.com/t/ansible-overwriting-file-lineinfile-when-run-in-parallel/30645/5 "2019-06-27T06:01:22Z")

</div>

> Hi All,
> 
> I am running the below code in a playbook which iterates over many hosts.  
> This script writes the value of ver.stdout to a file. I am seeing issue  
> when I run in in parallel. It is overwriting. It works good when serial:1  
> but it consumes long time since we have large number of hosts.
> 
> Any help to run in parallel and improve the time consumption?

It's not possible to write to the same file in parallel so you need to serialize it.  
Instead of doing it on the play, do it on the task.

> - name: Write version output  
> &nbsp;&nbsp;local\_action: lineinfile create=yes line={{ item }} insertafter=EOF  
> dest={{ dest\_file }}  
> &nbsp;&nbsp;when: ver.stdout != ""  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ ver.stdout | regex\_replace('\r\n' , '#' )}},{{ inventory\_hostname  
> }}"

Loop through all host in the play with ansible\_play\_hosts and use hostvars to get the variable and set run\_once as true.  
So something like this should work.

- name: Write version output  
&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '{{ hostvars[item].ver.stdout | regex\_replace('\r\n' , '#' )}},{{ item }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter: EOF  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: {{ dest\_file }}  
&nbsp;&nbsp;&nbsp;when: hostvars[item].ver.stdout != ""  
&nbsp;&nbsp;&nbsp;run\_once: yes  
&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;with\_items: '{{ ansible\_play\_hosts }}'
