# How to reference previous play's hostname and host\_vars in a jinja2 template

**URL:** <https://forum.ansible.com/t/how-to-reference-previous-plays-hostname-and-host-vars-in-a-jinja2-template/29810>\
**Category:** Ansible Project\
**Created:** [March 2, 2019, 3:38am UTC](https://forum.ansible.com/t/how-to-reference-previous-plays-hostname-and-host-vars-in-a-jinja2-template/29810 "2019-03-02T03:38:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bharath\_Kumar2](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Bharath\_Kumar2](https://forum.ansible.com/u/Bharath_Kumar2)\
**Post date:** [March 2, 2019, 3:38am UTC](https://forum.ansible.com/t/how-to-reference-previous-plays-hostname-and-host-vars-in-a-jinja2-template/29810/1 "2019-03-02T03:38:51Z")

</div>

Hello Ansible experts, I am having a ‘hostgroup’ with a ‘host\_var’ associated with each host of the ‘hostgroup’. Play 1 which targets the ‘hostgroup’, will create files on the target servers using the associated ‘host\_var’ and get the files back to ansible control machine and zip/archive the files. Play 2 which runs local to ansible control machine, needs to check for the files created, file name is ‘targethostname.txt’, and then create an email template with a table containing successful hosts and their associated ‘host\_var’ using jinja2 and then send an email with a table of list of targethostnames and their associated host\_vars.

I have completed the code for generating and copying the files to ansible control machine, zip, create a html template and send an email. I am struck at referencing/generating the html template file with a table consisting of the successful ‘targethostnames’ and their associated ‘host\_var’ in the html template. Can anyone please help me with an approach to achieve the list of successful targethostnames and their host\_vars in a jinja2 template. Thanks in advance.

`

{% for host in ansible\_play\_batch %}

{% endfor %}

| HOSTNAME / FQDN | HOST VAR |
| --- | --- |
| {{ inventory\_hostname }} | {{ host\_var }} |

`

The above doesn’t seem to work. (Also, previously I had both the tasks - now Play 1 and Play 2 - in a single play and had to use the ansible\_play\_batch, as above, but, neither that didn’t work.)
