ansible - variables reuse in templates

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:

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