# Updating variable in Ninja for loop

**URL:** <https://forum.ansible.com/t/updating-variable-in-ninja-for-loop/26843>\
**Category:** Ansible Project\
**Created:** [February 24, 2018, 1:08pm UTC](https://forum.ansible.com/t/updating-variable-in-ninja-for-loop/26843 "2018-02-24T13:08:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willem\_D\_Haese](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@Willem\_D\_Haese](https://forum.ansible.com/u/Willem_D_Haese)\
**Post date:** [February 24, 2018, 1:08pm UTC](https://forum.ansible.com/t/updating-variable-in-ninja-for-loop/26843/1 "2018-02-24T13:08:14Z")

</div>

Hello,

I’m trying to create a new string from the keys of a dictionary in a Ninja 2 template. But the variable I’m updating in the for loop seems to reset when the for loop finishes… The following piece of template is just a test to try demonstrate my issue:

`  
{% set ssh\_allowusers = ‘root’ %}  
{% for x in dictusers %}  
{{ssh\_allowusers }} + {{ x }}  
{% set ssh\_allowusers = ssh\_allowusers ~ ’ ’ ~ x %}  
{{ssh\_allowusers }}  
{% endfor %}  
Post Loop: {{ssh\_allowusers }}

`

dict\_users:

`dictusers: willemdh: shell: /bin/zsh groups: wheel shortname: Willem ansi: shell: /bin/sh groups: sysusers,wheel shortname: Willem`

But output is:

`root + ansi root ansi root ansi + willemdh root ansi willemdh Post Loop: root`

Why would the variable reset to ‘root’ after the loop?

Grtz

Willem

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 24, 2018, 1:46pm UTC](https://forum.ansible.com/t/updating-variable-in-ninja-for-loop/26843/2 "2018-02-24T13:46:47Z")

</div>

> I'm trying to create a new string from the keys of a dictionary in a Ninja

You probably mean Jinja and not Ninja 😉

> {% set ssh\_allowusers = 'root' %}  
> {% for x in dictusers %}  
> {{ssh\_allowusers }} + {{ x }}  
> {% set ssh\_allowusers = ssh\_allowusers ~ ' ' ~ x %}  
> {{ssh\_allowusers }}  
> {% endfor %}  
> Post Loop: {{ssh\_allowusers }}
> 
> dict\_users:
> 
> dictusers:  
> &nbsp;&nbsp;willemdh:  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/zsh  
> &nbsp;&nbsp;&nbsp;&nbsp;groups: wheel  
> &nbsp;&nbsp;&nbsp;&nbsp;shortname: Willem  
> &nbsp;&nbsp;ansi:  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/sh  
> &nbsp;&nbsp;&nbsp;&nbsp;groups: sysusers,wheel  
> &nbsp;&nbsp;&nbsp;&nbsp;shortname: Willem
> 
> But output is:
> 
> root + ansi  
> root ansi  
> root ansi + willemdh  
> root ansi willemdh  
> Post Loop: root
> 
> Why would the variable reset to 'root' after the loop?

A variable set in a block is not available outside the block.  
You can read more about it here  
[http://jinja.pocoo.org/docs/dev/templates/#assignments](http://jinja.pocoo.org/docs/dev/templates/#assignments)

---

<div class="post-metadata">

**Author:** ![Willem\_D\_Haese](https://avatars.discourse-cdn.com/v4/letter/w/b38774/32.png) [@Willem\_D\_Haese](https://forum.ansible.com/u/Willem_D_Haese)\
**Post date:** [February 24, 2018, 2:21pm UTC](https://forum.ansible.com/t/updating-variable-in-ninja-for-loop/26843/3 "2018-02-24T14:21:34Z")

</div>

Jinja it is 🙂

Solved it with:

AllowUsers {% set ssh\_allowusers = ‘root’ %}{% for x in dictusers %}{% set ssh\_allowusers = ssh\_allowusers ~ ’ ’ ~ x %}{% if loop.last %} {{ssh\_allowusers }} {% endif %}{% endfor %}
