# backreference to 'all hosts in scope of current task'?

**URL:** <https://forum.ansible.com/t/backreference-to-all-hosts-in-scope-of-current-task/17591>\
**Category:** Ansible Project\
**Created:** [February 22, 2015, 1:37pm UTC](https://forum.ansible.com/t/backreference-to-all-hosts-in-scope-of-current-task/17591 "2015-02-22T13:37:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 22, 2015, 1:37pm UTC](https://forum.ansible.com/t/backreference-to-all-hosts-in-scope-of-current-task/17591/1 "2015-02-22T13:37:29Z")

</div>

Hello,

I'd like to loop over all hosts that are in scope of the current task in  
a template.

Currently I use a fixed groupname as seen in the following block, but  
that will break if the scope doesn't match the groupname.

Is there a way to say loop over all hosts in scope of current task?

{% for host in groups['groupname'] %}  
&nbsp;&nbsp;{% for item in hostvars[host]['varfoo']['results'] -%}$  
&nbsp;&nbsp;&nbsp;&nbsp;{{ item.stdout }},  
&nbsp;&nbsp;{%- endfor %}  
{% endfor %}

thanks,  
Nusenu

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 22, 2015, 1:42pm UTC](https://forum.ansible.com/t/backreference-to-all-hosts-in-scope-of-current-task/17591/2 "2015-02-22T13:42:39Z")

</div>

​The special variable​ 'play\_hosts' is what you are looking for.

[http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 22, 2015, 1:55pm UTC](https://forum.ansible.com/t/backreference-to-all-hosts-in-scope-of-current-task/17591/3 "2015-02-22T13:55:46Z")

</div>

Serge van Ginderachter:

> > Is there a way to say loop over all hosts in scope of current  
> > task?
> 
> ​The special variable​ 'play\_hosts' is what you are looking for.
> 
> [http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

thanks!

> 

(and sorry for missing it when reading the documentation)
