# Omit item in list

**URL:** <https://forum.ansible.com/t/omit-item-in-list/32513>\
**Category:** Ansible Project\
**Created:** [March 23, 2020, 4:42pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513 "2020-03-23T16:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 23, 2020, 4:42pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513/1 "2020-03-23T16:42:29Z")

</div>

Hi !  
Is there any way to omit a item in a list at the declaration ?

- name: “Test”  
hosts: localhost  
vars:  
test1: false  
test2:
- name: test21  
includes:
- { file: “file221.j2”, path: “/somewhere/” }
- { file: “file222.j2”, path: “/somewhere/” }
- “{{ ( test1 | ternary ({ ‘file’: ‘file223.j2’, ‘path’: ‘/somewhere/’ }, **omit** ) ) }}”  
tasks:
- debug:  
var: test2
- debug:  
msg: “{{ item.1.path + item.1.file }}”  
with\_subelements:
- “{{ test2 }}”
- “includes”

---

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 23, 2020, 5:03pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513/2 "2020-03-23T17:03:05Z")

</div>

I would like, if it’s possible, to not change the task itself, only the variables.

Thank you,  
Have a nice day even in these difficult times

---

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 23, 2020, 5:13pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513/3 "2020-03-23T17:13:48Z")

</div>

It’s look like I misunderstood omit behavior and there is no way to do what I’m trying to do : [https://github.com/ansible/ansible/issues/22417](https://github.com/ansible/ansible/issues/22417)

---

<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:** [March 23, 2020, 5:50pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513/4 "2020-03-23T17:50:22Z")

</div>

Adding the item conditionally seems to be easier, I think

vars:  
&nbsp;&nbsp;&nbsp;&nbsp;test1: false  
&nbsp;&nbsp;&nbsp;&nbsp;list1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {file: "file221.j2", path: "/somewhere/"}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {file: "file222.j2", path: "/somewhere/"}  
&nbsp;&nbsp;&nbsp;&nbsp;list2: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if test1 %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{{ list1 }} + [{'file': 'file223.j2', 'path': '/somewhere/'}]]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ list1 }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}  
&nbsp;&nbsp;&nbsp;&nbsp;test2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: test21  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;includes: "{{ list2 }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 23, 2020, 6:20pm UTC](https://forum.ansible.com/t/omit-item-in-list/32513/5 "2020-03-23T18:20:31Z")

</div>

Thanks Vlad for your proposition.  
Excuse me for don't be enough precise in my first comment, but it's a more complex dict (than the example) and I can't split in two lists, it have to be one list and some items which could be undefined/omit.  
I'm not the only one to use those variables and the includes have to be in the same list (the one mandatory as the the optional).
