# how to get values from dict in template with for loop ?

**URL:** <https://forum.ansible.com/t/how-to-get-values-from-dict-in-template-with-for-loop/35524>\
**Category:** Ansible Project\
**Created:** [July 20, 2021, 3:03am UTC](https://forum.ansible.com/t/how-to-get-values-from-dict-in-template-with-for-loop/35524 "2021-07-20T03:03:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [July 20, 2021, 3:03am UTC](https://forum.ansible.com/t/how-to-get-values-from-dict-in-template-with-for-loop/35524/1 "2021-07-20T03:03:41Z")

</div>

I have two dict file like

teams.yml

{  
“teams”: [{“gid”: 10125,“location”: [“hq”],“name”: “aa”,“users”: [“chenhu”,“chouf”]},  
{“gid”: 10126,“location”: [“hq”],“name”: “ac”,“users”: [“guoj”,“yangc”]} ]

}

users.yml  
{

“users”: [{“name”: “changc”,“uid”: 20000},  
{“name”: “changb”,“uid”: 20001} ]  
}

I want to generate a file using template , so I start with two seperate for loop for test  
{% for i in users %}  
{{ i.uid }} {{ i.name }}  
{% endfor %}

{% for j in teams %}  
{{ j.users }} {{ j.name }}  
{% endfor %}

and ansible run with errors  
**AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘users’**

but teams dictonary did have users attr , why ansible can not get the value ??

so if I want to create a for loop in jinja2 template the condition is  
when user’s name show in teams then write down user’s name and teams name  
how do I do it ?  
{% for i in users %}  
{for j in teams %}  
{if i.name in j.users %}  
{{ i.name }}\_{{ j.name }}  
{% endif %}  
{% endfor %}  
{% endfor %}

that’s my first thought , but I have no idea how to do now …  
any suggestions ??
