# The List manipulation doesnt work

**URL:** <https://forum.ansible.com/t/the-list-manipulation-doesnt-work/32032>\
**Category:** Ansible Project\
**Created:** [January 22, 2020, 9:19pm UTC](https://forum.ansible.com/t/the-list-manipulation-doesnt-work/32032 "2020-01-22T21:19:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 22, 2020, 9:19pm UTC](https://forum.ansible.com/t/the-list-manipulation-doesnt-work/32032/1 "2020-01-22T21:19:28Z")

</div>

Hi Ansible Gurus,

I have created a list and from that list i want to prepare some customise list as per my need.  
But when i execute my playbook i get at last step when i want to see my customised list i get an error .  
Can someone guide me where i am doing wrong.

TASK [Display Lists] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={ best\_fav }}) =\> {  
“msg”: “{ best\_fav }}”  
}  
ok: [localhost] =\> (item=[AnsibleUndefined]) =\> {  
“msg”: “[AnsibleUndefined]”  
}  
ok: [localhost] =\> (item=[AnsibleUndefined]) =\> {  
“msg”: “[AnsibleUndefined]”  
}

My playbook is as follows

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 23, 2020, 5:58am UTC](https://forum.ansible.com/t/the-list-manipulation-doesnt-work/32032/2 "2020-01-23T05:58:14Z")

</div>

Kindly Help me on this as i am stuck

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 23, 2020, 8:16am UTC](https://forum.ansible.com/t/the-list-manipulation-doesnt-work/32032/3 "2020-01-23T08:16:48Z")

</div>

> \> - hosts: localhost  
> \> tasks:  
> \> - name: Create a list  
> \> set\_fact:  
> \> some\_value:  
> \> - avenger  
> \> - mk11  
> \> - witcher  
> \> - gow  
> \> - saintsrow  
> \>  
> \> - debug:  
> \> msg: "{{ some\_value }}"  
> \>  
> \> - name: Print avenger  
> \> set\_fact:  
> \> avenger\_record: "{{ item }}"  
> \> when: item is defined and item == 'avenger'  
> \> with\_items: "{{ somevalue }}"

The name of the variable is 'some\_value'. Correct other references as well

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ some\_value }}"

> \> [...]  
> \> - name: Display Lists  
> \> debug:  
> \> msg: "{{ item }}"  
> \> with\_items:  
> \> - "{ best\_fav }}"  
> \> - "{{ good\_fav }}"  
> \> - "{{ better\_fav }}"  
> \> when: item != None

Balance the braces. Correct

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ best\_fav }}"

Notes:

\* Use 'loop' instead of 'with\_items'. See  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/porting\_guides/porting\_guide\_2.5.html#migrating-from-with-x-to-loop](https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.5.html#migrating-from-with-x-to-loop)  
\* The variable 'item' is always defined in a loop (when not explicitly  
&nbsp;&nbsp;renamed). The condition 'when: item is defined ...' is redundant. It is  
&nbsp;&nbsp;enough to test e.g. 'when: item == "avenger"'  
\* The condition 'when: item != None' in the last task is also redundant. An  
&nbsp;&nbsp;iteration will be skipped if the list is empty.  
\* Give a try to 'ansible-lint' and 'yamllint'

HTH,

&nbsp;&nbsp;-vlado
