# Accessing play.hosts from a play

**URL:** <https://forum.ansible.com/t/accessing-play-hosts-from-a-play/14482>\
**Category:** Ansible Project\
**Created:** [February 4, 2014, 2:58am UTC](https://forum.ansible.com/t/accessing-play-hosts-from-a-play/14482 "2014-02-04T02:58:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Zarecor](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@Ed\_Zarecor](https://forum.ansible.com/u/Ed_Zarecor)\
**Post date:** [February 4, 2014, 2:58am UTC](https://forum.ansible.com/t/accessing-play-hosts-from-a-play/14482/1 "2014-02-04T02:58:12Z")

</div>

Is there a way to access the play.hosts from my play?

I would like to be able to do:

- hosts: tag\_role\_cluster\_host:&tag\_environment\_staging:&tag\_deployment\_us\_east

and then use the host list generated by that set intersection to configure each of hosts with the complete list of it’s peers.

So that in a jinja template I could do something like:

{%- for host in play\_hosts -%}  
cluster-add-host {{ host }}  
{%- endfor %}

I have other ways of accomplishing this, but they are not a clean as this and given that plays already provide set operators, I’d like to take advantage of them.

Ed.

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [February 4, 2014, 4:37am UTC](https://forum.ansible.com/t/accessing-play-hosts-from-a-play/14482/2 "2014-02-04T04:37:27Z")

</div>

hostvars.keys() will give you all the hosts for a given play.

-jlk
