# Create a varaible list conditionally from dictionary items

**URL:** <https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266>\
**Category:** Ansible Project\
**Created:** [May 20, 2015, 2:10am UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266 "2015-05-20T02:10:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [May 20, 2015, 2:10am UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/1 "2015-05-20T02:10:14Z")

</div>

Hi,

I’ve tried searching everything but can’t find a possible solution. I have a yml that defines all my variables for a playbook and I need to do something like the following inside of it:

---

<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:** [May 20, 2015, 1:20pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/2 "2015-05-20T13:20:14Z")

</div>

I think you want something like:

my\_list: “{{ my\_dict|selectattr(‘item\_en’)|map(attribute=‘name’)|list }}”

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [May 20, 2015, 4:54pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/3 "2015-05-20T16:54:48Z")

</div>

Matt’s solution is fine (and elegant!) for simple cases like this particular one. However, I found myself needing to filter items on some more complex conditions a few times and started using the Jinja do extension to effectively turn variable assignments into arbitrary computations:

`  
my\_list: |  
{%- set names = -%}  
{%- for item in my\_dict if item.item\_en -%}  
{%- do names.append(item.name) -%}  
{%- endfor -%}  
{{ names }}

`

It’s important to do proper whitespace-control (the dashes in {%- … -%}) because in the end the string assigned to my\_list must be “[‘name1’, ‘name3’]” and not something like " [‘name1’, ‘name3’]“. In the former case, Ansible converts it to a proper list (because the value starts with ‘[’), in the latter my\_list would be assigned the actual string " […]”.

And you need to enable the Jinja extension in your ansible.cfg:

`  
[defaults]  
jinja2\_extensions = jinja2.ext.do

`

As mentioned in the beginning, this isn’t really necessary in this case and certainly isn’t very much in Ansible’s spirit of simplicity, but it has saved me from duplicating information across multiple variables several times.

---

<div class="post-metadata">

**Author:** ![Shawn\_Ferry](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Shawn\_Ferry](https://forum.ansible.com/u/Shawn_Ferry)\
**Post date:** [May 20, 2015, 6:37pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/4 "2015-05-20T18:37:26Z")

</div>

Why not something like this?

do\_something\_with: item.name  
with\_items: your\_dict  
when: item.item\_en == True

---

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [May 20, 2015, 6:55pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/5 "2015-05-20T18:55:19Z")

</div>

Thanks guys,

Christian is your solution doable in a regular yml? I thought jinja constructs were only allowed in jinja templates? What I have is a regular .yml that defines all my variables.

Shawn I believe your solution requires creating a task to loop over my\_dict, I prefer to do it outside of the playbook.

---

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [May 20, 2015, 7:19pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/6 "2015-05-20T19:19:53Z")

</div>

Also, another quick question. If I was using my\_dict in a task loop, could i put a condition on:

with\_items: my\_dict  
when: item.key is defined

will that work?

---

<div class="post-metadata">

**Author:** ![Shawn\_Ferry](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Shawn\_Ferry](https://forum.ansible.com/u/Shawn_Ferry)\
**Post date:** [May 20, 2015, 7:50pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/7 "2015-05-20T19:50:03Z")

</div>

> Also, another quick question. If I was using my\_dict in a task loop, could i put a condition on:

Right that is my example. Here it is in code when item\_en is false the item is skipped.

TASK: [Loop] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [127.0.0.1] =\> (item={‘key’: ‘value1’, ‘type’: ‘type1’, ‘name’: ‘name1’, ‘item\_en’: True})  
skipping: [127.0.0.1] =\> (item={‘key’: ‘value2’, ‘type’: ‘type2’, ‘name’: ‘name2’, ‘item\_en’: False})  
changed: [127.0.0.1] =\> (item={‘key’: ‘value3’, ‘type’: ‘type3’, ‘name’: ‘name3’, ‘item\_en’: True})

[https://gist.github.com/shawnferry/572c4f733a3f06f86a37](https://gist.github.com/shawnferry/572c4f733a3f06f86a37)

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [May 20, 2015, 7:54pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/8 "2015-05-20T19:54:35Z")

</div>

Yes, I have that stuff in my group\_vars/all and in various vars.yml files. There no special magic to it except that you have to enable the do extension in ansible.cfg.

Every string variable in Ansible is fed through the templating engine:

`

---

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [May 20, 2015, 8:35pm UTC](https://forum.ansible.com/t/create-a-varaible-list-conditionally-from-dictionary-items/18266/9 "2015-05-20T20:35:47Z")

</div>

Right thanks guys. And what about using a condition that checks if my\_dict contains a entry with name == name2 ? Its not a loop, just a conditional for file include so I need a one liner.
