On Tue, 22 Jun 2021 23:08:50 +0200
Gaétan QUENTIN@Work <
work.gaet...@gmail.com> wrote:
> - name: set item_ok
> set_fact:
> item_ok: false
> - name: call included task for each items
> include_tasks: "tests_on_item.yml"
> loop: "{{ item_list }}"
> loop_control:
> loop_var: outer_item
> when: item_ok is false
It's not possible to break a loop. In some cases you can use
registered value to skip the rest of the list. But, there might be a
solution to your use-case: "Find the first item in a list that meets
a list of conditions". For example, given the list
_list:
- {a1: A, a2: 1, a3: first}
- {a1: A, a2: 2, a3: second}
- {a1: B, a2: 3, a3: third}
- {a1: A, a2: 2, a3: last}
find the first item that meets the conditions
- a1 == 'A'
- a2 == 2
The task
- set_fact:
x: "{{ _list|
selectattr('a1', 'eq', 'A')|
selectattr('a2', 'eq', 2)|
first }}"
gives
x:
a1: A
a2: 2
a3: second
If you need the index of the item
- set_fact:
i: "{{ _list.index(x) }}"
gives
i: '1'
The next option is *json_query*. The task below gives the same result
- set_fact:
x: "{{ _list|json_query(query)|first }}"
vars:
query: "[?a1 == 'A']|
[?a2 == `2`]"
A custom filter might be the next option to improve the efficiency of
searching large lists.
--
Vladimir Botka