Omit item in list

246 views
Skip to first unread message

Quentin Aveno

unread,
Mar 23, 2020, 12:42:29 PM3/23/20
to Ansible Project
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"

Quentin Aveno

unread,
Mar 23, 2020, 1:03:06 PM3/23/20
to Ansible Project
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


Le lundi 23 mars 2020 17:42:29 UTC+1, Quentin Aveno a écrit :
Hi !
Is there any way to omit a item in Citer le message d'originea list at the declaration ?

Quentin Aveno

unread,
Mar 23, 2020, 1:13:48 PM3/23/20
to Ansible Project
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

Vladimir Botka

unread,
Mar 23, 2020, 1:50:43 PM3/23/20
to Quentin Aveno, ansible...@googlegroups.com
On Mon, 23 Mar 2020 09:42:29 -0700 (PDT)
Quentin Aveno <aori...@gmail.com> wrote:

> 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*) ) }}"

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

Quentin Aveno

unread,
Mar 23, 2020, 2:20:32 PM3/23/20
to Ansible Project
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).
Reply all
Reply to author
Forward
0 new messages