loop over dict2items

131 views
Skip to first unread message

fusillator

unread,
Feb 2, 2019, 12:16:52 PM2/2/19
to Ansible Project
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

fusillator

unread,
Feb 2, 2019, 2:03:23 PM2/2/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages