I would like to compare more than 2 lists, and see if they have the same values in same indexes.
I have the previous questions regarding how I can alternate the values in the multiple lists. here Based on this, I need to compare each values and stop if there is value non-identical. (There might be better solution, please welcome to add)
---
- name: data test
hosts: localhost
vars:
data_1: [True, False, True]
data_2: [False, True, True]
tasks:
- name: combine
set_fact:
comp_data: "{{ comp_data | default([]) + [item] }}"
loop:
- "{{ data_1 }}"
- "{{ data_2 }}"
- name: list all comp_data
debug:
msg: "{{ comp_data }}"
- name: zip
set_fact:
zip_data: "{{ lookup('together', *comp_data) }}"
- name: list all zip comp_data
debug:
msg: "{{ zip_data }}"
- name: check each element are identical
assert:
that:
- "{{ item | unique | length == 1 }}"
msg: |
"Data is not identical"
"{{ item }}"
loop: "{{ zip_data }}"
Output:
TASK [check each element are identical] *************************************************************************************************************************************************
failed: [localhost] (item=[True, False]) => {
"ansible_loop_var": "item",
"assertion": false,
"changed": false,
"evaluated_to": false,
"item": [
true,
false
]
}
MSG:
"Data is not identical"
"[True, False]"
failed: [localhost] (item=[False, True]) => {
"ansible_loop_var": "item",
"assertion": false,
"changed": false,
"evaluated_to": false,
"item": [
false,
true
]
}
MSG:
"Data is not identical"
"[False, True]"
ok: [localhost] => (item=[True, True]) => {
"ansible_loop_var": "item",
"changed": false,
"item": [
true,
true
]
}
MSG:
All assertions passed
At the end, it should stop as soon as it sees non-identical, but it iterates until the last list.
Any idea how I can stop right away and better way to compare lists if any?
Thank you,