Ansible Playbook output to local file

32 views
Skip to first unread message

Chris Bidwell - NOAA Federal

unread,
Mar 8, 2019, 11:03:35 AM3/8/19
to ansible...@googlegroups.com
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:

---
- 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

Kai Stian Olstad

unread,
Mar 8, 2019, 11:38:03 AM3/8/19
to ansible...@googlegroups.com
On 08.03.2019 17:03, 'Chris Bidwell - NOAA Federal' via Ansible Project wrote:
> 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.

--
Kai Stian Olstad

Chris Bidwell - NOAA Federal

unread,
Mar 8, 2019, 1:23:32 PM3/8/19
to ansible...@googlegroups.com
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:

--
- hosts: ALL_RHEL
  become: yes
  vars_files:
    - passwd.yml
    - vars.yml

  vars:
    - OUTPUT: "/home/chris.bidwell/ansible/roles/output.txt"

  tasks:
    - name: cleanup
      shell: "rm -rf {{ OUTPUT }}"

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

    - name: Output info to OUTPUT.txt
      lineinfile:
        dest: "{{ OUTPUT }}"
        line: "{{ hostvars[item].ansible_hostname }}, {{ hostvars[item].ansible_kernel }}, {{ hostvars[item].rh_type.stdout }}, {{ hostvars[item].facter_virtual }}"
        create: yes
      delegate_to: localhost
      run_once: yes
      with_items: "{{ ansible_play_hosts }}"
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/64231d7a-9185-6cd2-4d74-a35049e64a98%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages