# ansible - variables reuse in templates

**URL:** <https://forum.ansible.com/t/ansible-variables-reuse-in-templates/19548>\
**Category:** Ansible Project\
**Created:** [October 19, 2015, 2:14pm UTC](https://forum.ansible.com/t/ansible-variables-reuse-in-templates/19548 "2015-10-19T14:14:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavlo\_Zhuk](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@Pavlo\_Zhuk](https://forum.ansible.com/u/Pavlo_Zhuk)\
**Post date:** [October 19, 2015, 2:14pm UTC](https://forum.ansible.com/t/ansible-variables-reuse-in-templates/19548/1 "2015-10-19T14:14:29Z")

</div>

Dears,

I am trying to construct a variable inside the loop of ansible template. The value gets constructed, but I’m unable to use it outside the loop.  
Looks like variables with same name inside the loop and outside the loop are pointing to actually different variables.

Is there any way to construct a varialbe inside the loop, and use it outside?

Example playbook:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 19, 2015, 2:21pm UTC](https://forum.ansible.com/t/ansible-variables-reuse-in-templates/19548/2 "2015-10-19T14:21:46Z")

</div>

Jinja2 has some unexpected variable scopes when it comes to loops. You are setting mynewvar inside of a scope for a for loop, which is not available outside of the for loop.

There is some explanation at a similar issue logged on the jinja2 project: [https://github.com/mitsuhiko/jinja2/issues/330](https://github.com/mitsuhiko/jinja2/issues/330)
