# Collect together the results of all hosts

**URL:** <https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608>\
**Category:** Get Help\
**Tags:** fedora, playbook, jinja2\
**Created:** [October 31, 2024, 9:56am UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608 "2024-10-31T09:56:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [October 31, 2024, 9:56am UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/1 "2024-10-31T09:56:31Z")

</div>

Hi,

How can I with ansible collect all results not only from one host but from all and put together in a file (template)?

Here’s below my playbook and jinja2 config’s files :

```auto
---
- name: Get all interfaces
  hosts: fedora
  gather_facts: false
  remote_user: ansible
  vars:
    all_hosts_interfaces: {}

  tasks:
    - name: Get all interfaces of hosts
      raw: "nmcli con show | sed '1 d' | awk '{print $1}'"
      register: nmcli_ports

    - name: Create dict of interfaces
      set_fact:
        all_hosts_interfaces: "{{ dict([['host_name', inventory_hostname],['int_name', nmcli_ports.stdout_lines]]) }}"
    
    - name: Create file with template
      template:
        src: all_int_report4.j2
        dest: "all_int_report4.txt"
      delegate_to: localhost
      vars:
        all_interfaces: "{{ all_hosts_interfaces }}"

```

```auto
# Create by ansible
# ========================================

hostname : {{ all_interfaces.host_name }}
{% for item in all_interfaces.int_name %}
  net: {{ item }}
{% endfor %}

```

Here’s the result that I got:

```auto
# Create by ansible
# ========================================

hostname : x.x.x.x
  net: ens160
  net: lo

```

and what I’d like to get:

```auto
# Create by ansible
# ========================================

hostname : x.x.x.w
  net: ens160
  net: lo
hostname : x.x.x.y
  net: ens160
  net: lo
hostname : x.x.x.x
  net: ens160
  net: lo
hostname : x.x.x.z
  net: ens160
  net: lo

```

if I understand correctly, the creation of the dict is renewed for each host, so there won’t be any append to the dict. the values will always be those of the last hosts

I think Ansible isn’t designed for this kind of thing and I need to find another solution, or do you have any idea?

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 31, 2024, 1:42pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/2 "2024-10-31T13:42:56Z")

</div>

You can definitely do this with Ansible.  
Template:

```yaml
{{ ansible_managed | comment }}

{% for host in ansible_play_hosts %}
hostname : {{ host }}
{% for item in hostvars[host]['all_hosts_interfaces']['int_name'] %}
  net: {{ item }}
{% endfor %}
{% endfor %}

```

Task:

```yaml
    - name: Create file with template
      ansible.builtin.template:
        src: all_int_report4.j2
        dest: "all_int_report4.txt"
      delegate_to: localhost
      run_once: true

```

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [October 31, 2024, 2:21pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/3 "2024-10-31T14:21:41Z")

</div>

Thank you for your quick answer… yeah it’s running well 😉  
Is it because you call the hostvars variable? In other words, you loop through all the hosts, instead only of variable like my example?  
Apart the run\_once I see only this difference.

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 31, 2024, 3:10pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/4 "2024-10-31T15:10:22Z")

</div>

Good question.

`{% for host in ansible_play_hosts %}` will loop through all the hosts in the playbook and then you can use `hostvars` to access variables that belong to the hosts.

With this approach, you only want to run the task once, so I use `run_once` in conjunction with `delegate_to: localhost` to achieve this.

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 31, 2024, 3:14pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/5 "2024-10-31T15:14:23Z")

</div>

You might also want to consider replacing `ansible.builtin.raw` with `ansible.builtin.shell`, unless your hosts does not have Python installed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 30, 2024, 3:14pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608/6 "2024-11-30T15:14:50Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
