# loop over dict2items

**URL:** <https://forum.ansible.com/t/loop-over-dict2items/29602>\
**Category:** Ansible Project\
**Created:** [February 2, 2019, 5:16pm UTC](https://forum.ansible.com/t/loop-over-dict2items/29602 "2019-02-02T17:16:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 2, 2019, 5:16pm UTC](https://forum.ansible.com/t/loop-over-dict2items/29602/1 "2019-02-02T17:16:52Z")

</div>

```auto
Hi all, trying the following playbook taken from user_guide/playbooks_loops.html 

- hosts: localhost
  tasks:
  - name: create a tag dictionary of non-empty tags
    set_fact:
      tags_dict: "{{ (tags_dict|default({}))|combine({item.key: item.value}) }}"
    loop: "{{ tags|dict2items }}"
    vars:
      tags:
        Environment: dev
        Application: payment
        Another: "{{ doesnotexist|default() }}"
    when: item.value != ""

I got the error 

$ ansible-playbook dict2items.yml 

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [create a tag dictionary of non-empty tags] ******************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "dict2items requires a dictionary, got <class 'jinja2.runtime.StrictUndefined'> instead."}
	to retry, use: --limit @/home/fusillator/Code/ansible/dict2items.retry

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1   

$ ansible-playbook --version
ansible-playbook 2.7.6

Any hint?

thanks 

Luca 

```

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 2, 2019, 7:03pm UTC](https://forum.ansible.com/t/loop-over-dict2items/29602/2 "2019-02-02T19:03:23Z")

</div>

```auto
I figure out that the problem was related to the variable name (tags) 
the name tags is also used for the special attribute tags useful for filtering tasks to be executed.
Anyway changing the snippet using mytags instead of tags solves the error.

- hosts: localhost
  tasks:
  - name: create a tag dictionary of non-empty tags
    set_fact:
      tags_dict: "{{ (tags_dict|default({}))|combine({item.key: item.value}) }}"
    loop: "{{ mytags|dict2items }}"
    vars:
      mytags:
        Environment: dev
        Application: payment
        Another: "{{ doesnotexist|default() }}"
    when: item.value != ""

Please consider to update the manual

```
