Ansible - Compare Differences between Lists
by
Jeremy Canfield |
Updated: November 08 2023
| Ansible articles
The difference filter can be used to compare differences between lists.
For example, the foo_list contains "pears" whereas the bar_list contains "oranges".
---
- hosts: localhost
vars:
foo_list: [ "apple", "banana", "grapes", "pears"]
bar_list: [ "apple", "banana", "grapes", "oranges"]
tasks:
- name: foo_list differences
debug:
msg: "{{ foo_list | difference(bar_list) }}"
- name: bar_list differences
debug:
msg: "{{ bar_list | difference(foo_list) }}"
...
Should produce the following.
TASK [foo_list differences]
ok: [localhost] => {
"msg": [
"pears"
]
}
TASK [bar_list differences]
ok: [localhost] => {
"msg": [
"oranges"
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at