Bootstrap FreeKB - Ansible - Compare Differences between Lists
Ansible - Compare Differences between Lists

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter c490ea in the box below so that we can be sure you are a human.