# Can we rely on play variables being re-calculated/re-templated for each host in a play?

**URL:** <https://forum.ansible.com/t/can-we-rely-on-play-variables-being-re-calculated-re-templated-for-each-host-in-a-play/22115>\
**Category:** Ansible Project\
**Created:** [July 25, 2016, 8:04pm UTC](https://forum.ansible.com/t/can-we-rely-on-play-variables-being-re-calculated-re-templated-for-each-host-in-a-play/22115 "2016-07-25T20:04:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Strootman](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@Jonathan\_Strootman](https://forum.ansible.com/u/Jonathan_Strootman)\
**Post date:** [July 25, 2016, 8:04pm UTC](https://forum.ansible.com/t/can-we-rely-on-play-variables-being-re-calculated-re-templated-for-each-host-in-a-play/22115/1 "2016-07-25T20:04:54Z")

</div>

Hello,

I know that you _can_, but it seems ambiguous on whether one _should_.

For example:  
There is a role named “role\_1”, which has templated variables in both “defaults/main.yml” and “vars/main.yml”.  
I could create a playbook which prints those variables for a set of hosts, and the variables would change for each host.  
For the sake of discussion, here is what those role variables could be:

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 28, 2017, 4:19pm UTC](https://forum.ansible.com/t/can-we-rely-on-play-variables-being-re-calculated-re-templated-for-each-host-in-a-play/22115/2 "2017-02-28T16:19:22Z")

</div>

Variables are lazily evaluated and computed per host. The issues with  
set\_fact are that the evaluation is happening before setting the  
variable as ansible evaluates the expression to pass it to the task:

so

vars:  
varname: "{{expression}}" # this is lazy

- set\_facts: varname={{expression}} #var is set to result of  
'expression' as it is evaluated for the task.

I hope that clarifies the issue.
