Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Ansible check if list is in list

Here's my hack for how to check if a list's items are all in another list.

  - name: learn current mounts
    shell: mount | grep -iE "cifs|nfs" | grep -viE '^\s*#|pipefs|proc\/fs\/nfsd' | awk '{print $3}' | sort
    args:
      warn: no
    register: mounts

  - name: learn requested mounts
    shell: grep -iE "cifs|nfs" /etc/fstab | grep -viE '^\s*#|pipefs|proc\/fs\/nfsd' | awk '{print $2}' | sort
    args:
      warn: no
    register: fstab_entries

  - name: fail when fstab_entries not in mounts
    debug:
      msg: "{{ item }}"
    failed_when:
    - 'item not in mounts.stdout_lines'
    loop: "{{ fstab_entries.stdout_lines | flatten(levels=1) }}"

I am just using a debug task that loops over all the items of the possibly shorter list, with a condition to fail when the item is not in the longer list.

Comments