i have good knowledge at programming langage, but there's something I can't make with Ansible filters and YAML syntax. Basically, I want to iterate over array/lists and make a comparison of each elements, like that in C language :
for (i=0;i<a;i++) {
for (j=0;j<b;j++) {
if (array1[i]==array2[j]) {
....
}
}
}
In my case, i'm trying to compare each attributes of two lists one by one.
Is there anyway to do it with Ansible?
Thanks in advance for your answers.
For example
- hosts: localhost
vars:
array1: [a, b, c]
array2: [b, d, c, a]
tasks:
- debug:
msg: "{{ item.0 }} == {{ item.1 }} {{ item.0 == item.1 }}"
loop: "{{ array1|product(array2)|list }}"
gives
msg: a == b False
msg: a == d False
msg: a == c False
msg: a == a True
msg: b == b True
msg: b == d False
msg: b == c False
msg: b == a False
msg: c == b False
msg: c == d False
msg: c == c True
msg: c == a False
If you want to find indices
- debug:
msg: "array1[{{ array1.index(item.0) }}] ==
array2[{{ array2.index(item.1) }}]"
loop: "{{ array1|product(array2)|list }}"
when: item.0 == item.1
gives
msg: array1[0] == array2[3]
msg: array1[1] == array2[0]
msg: array1[2] == array2[2]