To test if a string contains a substring in Jinja, we use the in test, much like Python:
"somestring" in somevariable
In your case, you want to check if a given string contains any substring from the excluded list. Conceptually, what we want is something like the Python expression
if any(x in mount.device for x in excluded)
Using Jinja filters, we need to reverse our logic a little bit. We
can use the select filter to get a list of strings from the
excluded list that are contained in a given target string (such as
mount.device) like this:
excluded|select('in', item)
If item matches anything in the excluded list, the above
expression will result in a non-empty list (which evaluates to true
when used in a boolean context).
Used in a playbook, it would look like this:
- hosts: localhost
gather_facts: false
vars:
excluded: ['home', 'cdrom', 'tmpfs', 'sys', 'run', 'dev', 'root']
mounts:
- /dev/mapper/vgroot-local_home
- devtmpfs
- tmpfs
- something/else
tasks:
- debug:
msg: "unmount {{ item }}"
when: not excluded|select('in', item)
loop: "{{ mounts }}"
The above playbook produces as output:
TASK [debug] *******************************************************************
skipping: [localhost] => (item=/dev/mapper/vgroot-local_home)
skipping: [localhost] => (item=devtmpfs)
skipping: [localhost] => (item=tmpfs)
ok: [localhost] => (item=something/else) => {
"msg": "unmount something/else"
}
That is, it skips the task when the current loop item contains a
substring from the excluded list.
Assuming that your goal is "unmount all filesystems except those for
which the device name contains a substring from the excluded list",
you might write:
- name: Unmount filesystems that aren't excluded
ansible.posix.mount:
path: '{{ mount.device }}'
state: unmounted
when: not excluded|select('in', item.device)
loop: "{{ ansible_mounts }}"
become: true
tags: mountpoints