# How to access stdout from all hosts in a localserver

**URL:** <https://forum.ansible.com/t/how-to-access-stdout-from-all-hosts-in-a-localserver/29327>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 26, 2018, 4:10am UTC](https://forum.ansible.com/t/how-to-access-stdout-from-all-hosts-in-a-localserver/29327 "2018-12-26T04:10:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhav\_manne](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/madhav_manne/32/5308_2.png) [@madhav\_manne](https://forum.ansible.com/u/madhav_manne)\
**Post date:** [December 26, 2018, 4:10am UTC](https://forum.ansible.com/t/how-to-access-stdout-from-all-hosts-in-a-localserver/29327/1 "2018-12-26T04:10:57Z")

</div>

My playbook is as follows

- hosts: nodes  
become: yes  
tasks:
- name: Run Shell Script to get IPs with 4xx and 5xx errors

script: /home/ubuntu/ips.sh  
args:  
chdir: /home/ubuntu  
register: ips

- name:  
shell: echo “{{ hostvars[groups[‘nodes’][0]].ips.stdout}}” \> pip.txt  
delegate\_to: localhost

There are 10 ansible hosts. Is There a way I can access Ips.stdout from all 10 hosts from my local server. I’m able to get the first host by the above command. How can I access all 10 hosts stdout from a single variable?

---

<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:** [December 26, 2018, 10:14am UTC](https://forum.ansible.com/t/how-to-access-stdout-from-all-hosts-in-a-localserver/29327/2 "2018-12-26T10:14:08Z")

</div>

> My playbook is as follows
> 
> - hosts: nodes  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name: Run Shell Script to get IPs with 4xx and 5xx errors
> 
> &nbsp;&nbsp;&nbsp;&nbsp;script: /home/ubuntu/ips.sh  
> &nbsp;&nbsp;&nbsp;&nbsp;args:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chdir: /home/ubuntu  
> &nbsp;&nbsp;&nbsp;&nbsp;register: ips
> 
> &nbsp;&nbsp;- name:  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: echo "{{ hostvars[groups['nodes'][0]].ips.stdout}}" \> pip.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

Running shell like this will run the task as many times as it's hosts in nodes.  
Each one will overwrite the pip.txt file.

> There are 10 ansible hosts. Is There a way I can access Ips.stdout from all  
> 10 hosts from my local server. I'm able to get the first host by the above  
> command. How can I access all 10 hosts stdout from a single variable?

You can use a loop with with\_items like this

- debug: msg="{{ item.ips.stdout }}"  
&nbsp;&nbsp;with\_items: "{{ groups.nodes }}"  
&nbsp;&nbsp;delegate\_to: localhost

(just for the record, debug module always run on localhost, so with debug delegate\_to could be left out)

But still this task will still run as many times as there are hosts in nodes.  
To mitigate that you have "run\_once: true" that only run this task one time.

- debug: msg="{{ item.ips.stdout }}"  
&nbsp;&nbsp;with\_items: "{{ group.nodes }}"  
&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;run\_once: true

But since you would like to have this in a file you would need to use Jinja template and the copy module.  
So instead of looping with with\_items you do the loop in Jinga template.

- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for host in groups.nodes %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars[host].ips.stdout }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}}  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/pip.txt  
&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;run\_once: true
