Omit item in list

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”

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

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

Adding the item conditionally seems to be easier, I think

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

HTH,

  -vlado

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).