# looping over dict elements

**URL:** <https://forum.ansible.com/t/looping-over-dict-elements/19687>\
**Category:** Ansible Project\
**Created:** [November 4, 2015, 3:46pm UTC](https://forum.ansible.com/t/looping-over-dict-elements/19687 "2015-11-04T15:46:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [November 4, 2015, 3:46pm UTC](https://forum.ansible.com/t/looping-over-dict-elements/19687/1 "2015-11-04T15:46:27Z")

</div>

I have the following structure:

> “a”: {  
> “v1”: 1,  
> “v2”: “A”  
> }  
> “b”: {  
> “v1”: 2,  
> “v2”: “B”  
> }

It is read from an external json source using:

> ```auto
> - set_fact: envs="{{ external.stdout | from_json }}"
> 
> ```

I can loop/iterate over it only by using with\_dict:

> ```auto
> - debug: msg="Item is {{ item.key }} with value {{ item.value.v1}} and{{ item.value.v2}}"
> with_dict: envs
> 
> ```

but if I try anything else it throws exception:

> AttributeError: ‘unicode’ object has no attribute ‘items’

I tried the following:

> ```auto
> - set_fact: envs_list="{{ envs | dictsort }}"
> 
> ```

and:

> ```auto
> - debug: var=item
> with_items: envs.items()
> 
> ```

and inside template:

> ```auto
> {% for env in envs|dictsort %}
> 
> ```

and:

> ```auto
> {% for env, setting in envs.`iteritems`() %}
> 
> ```

…with the same result.

I would need to use that structure inside a template…

I’m using ansible 1.9.1 on CentOS 6

Any idea how to solve that?

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [November 4, 2015, 4:03pm UTC](https://forum.ansible.com/t/looping-over-dict-elements/19687/2 "2015-11-04T16:03:59Z")

</div>

UPDATE: Python 2.6.6

1. november 4., szerda 16:46:27 UTC+1 időpontban Bence Takács a következőt írta:

---

<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:** [November 4, 2015, 4:34pm UTC](https://forum.ansible.com/t/looping-over-dict-elements/19687/3 "2015-11-04T16:34:12Z")

</div>

Use `from_json` during the “last mile”, as the set\_fact is casting that output back to a string.

Such as:

```auto
{% for env in envs|from_json|dictsort %}

```

and drop `from_json` from your set\_fact. Or just use `external.stdout|from_json|dictsort` where you need it, and don’t use set\_fact.

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [November 4, 2015, 5:01pm UTC](https://forum.ansible.com/t/looping-over-dict-elements/19687/4 "2015-11-04T17:01:10Z")

</div>

Thanks, this works!

But in that case I need to put that expression everywhere I want to use that variable.

Is there a way to register new object variable while running the playbook?

1. november 4., szerda 17:34:59 UTC+1 időpontban Matt Martz a következőt írta:
