# Ansible Playbook output to local file

**URL:** <https://forum.ansible.com/t/ansible-playbook-output-to-local-file/29850>\
**Category:** Ansible Project\
**Created:** [March 8, 2019, 4:03pm UTC](https://forum.ansible.com/t/ansible-playbook-output-to-local-file/29850 "2019-03-08T16:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [March 8, 2019, 4:03pm UTC](https://forum.ansible.com/t/ansible-playbook-output-to-local-file/29850/1 "2019-03-08T16:03:11Z")

</div>

Hi all,

So I’ve got this playbook that is getting information from all of my hosts in my inventory and outputting that data to a local file. However, it’s not outputting on EVERY inventory host. It’s going through the list like it’s supposed to be, but it’s not putting everything in the file. Any ideas? I’ve got over 80 systems in my ALL\_RHEL group but only get about 40 line items in my output.txt file.

Here’s my playbook:

---

<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:** [March 8, 2019, 4:37pm UTC](https://forum.ansible.com/t/ansible-playbook-output-to-local-file/29850/2 "2019-03-08T16:37:52Z")

</div>

> Hi all,
> 
> So I've got this playbook that is getting information from all of my hosts  
> in my inventory and outputting that data to a local file. However, it's  
> not outputting on EVERY inventory host. It's going through the list like  
> it's supposed to be, but it's not putting everything in the file. Any  
> ideas? I've got over 80 systems in my ALL\_RHEL group but only get about 40  
> line items in my output.txt file.

All[1] the "hosts" is trying to write to the same file at the same the same time, and that is causing your problem.

> Here's my playbook:
> 
> ---  
> - hosts: ALL\_RHEL  
> &nbsp;&nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- passwd.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vars.yml
> 
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Workstation or Server?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: cat /etc/redhat-release | awk '{print $5}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: rh\_type  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: name
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Output info to output.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/output.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ ansible\_hostname }}, {{ ansible\_kernel }}, {{  
> rh\_type.stdout }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter: EOF  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

This last task you need to run as loop with run\_once so only one task is writing to the file.  
Something like this:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Output info to output.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/output.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ hostvars[item].ansible\_hostname }}, {{ hostvars[item].ansible\_kernel }}, {{ hostvars[item].rh\_type.stdout }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter: EOF  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ ansible\_play\_hosts }}"

[1] How may host is actually determine by fork and serial. Default it would be 5 hosts at a time.

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [March 8, 2019, 6:23pm UTC](https://forum.ansible.com/t/ansible-playbook-output-to-local-file/29850/3 "2019-03-08T18:23:08Z")

</div>

Thanks for that info! That’s helping quite a bit. I added another field to output and since adding that field, it’s only giving me about 70 systems instead of the full 93. Any idea on that one?  
Here’s my new playbook after implementing your recommendations:
