Ansible Playbook output to local file

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:

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
   become: yes
   vars_files:
     - passwd.yml
     - vars.yml

   tasks:
     - name: Workstation or Server?
       shell: cat /etc/redhat-release | awk '{print $5}'
       register: rh_type
       tags: name

     - name: Output info to output.txt
       lineinfile:
         dest: /tmp/output.txt
         line: "{{ ansible_hostname }}, {{ ansible_kernel }}, {{
rh_type.stdout }}"
         create: yes
         insertafter: EOF
       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:

    - name: Output info to output.txt
      lineinfile:
        dest: /tmp/output.txt
        line: "{{ hostvars[item].ansible_hostname }}, {{ hostvars[item].ansible_kernel }}, {{ hostvars[item].rh_type.stdout }}"
        create: yes
        insertafter: EOF
      delegate_to: localhost
      run_once: yes
      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.

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: