condition based on register match

I have a task that mounts devices in a Linux machine.
I wish to run the task only if the device is not mounted, therefore I’m running a mount command and save the output in a register.

On the mount task, I’m trying to use when by searching the device in the register output - and I’m failing to do so.
The task is running even that /dev/sdb1 is included in the register

Playbook:

  - name: check mount
    shell: mount | awk '{print $1}'
    register: mount

  - debug:
      msg: " {{ mount.stdout_lines }}"

  - name: Mount task 1 - create directory
    file:
      path: "{{item.mount_path}}"
      state: directory
    when: mount.stdout_lines != "/dev/sdb1"

Register output

sysfs\nproc\ndevtmpfs\nsecurityfs\ntmpfs\ndevpts\ntmpfs\ntmpfs\ncgroup\npstore\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\ncgroup\nconfigfs\n/dev/mapper/rhel-root\nselinuxfs\nsystemd-1\ndebugfs\nmqueue\nhugetlbfs\n/dev/sda1\n/dev/mapper/rhel-home\ntmpfs\n/dev/sdb1\n/dev/sdc1\n/dev/sdd1"

The module "mount" does the job and creates the directory if needed.
https://docs.ansible.com/ansible/latest/modules/mount_module.html

  "state: If mounted, the device will be actively mounted and appropriately
  configured in fstab. If the mount point is not present, the mount point
  will be created."

HTH,

  -vlado

Correct syntax is below

   - name: Mount task 1 - create directory
     file:
       path: /dev/sdb1
       state: directory
     when: "'/dev/sdb1' not in mount.stdout_lines"

The code implicates a loop

   - name: Mount task 1 - create directory
     file:
       path: "{{ item.mount_path }}"
       state: directory
     loop: "{{ my_list_of_mountpoints }}"
     when: "item.mount_path not in mount.stdout_lines"

(Ansible module "mount" does the job.)

HTH,

  -vlado

Errata:

Correct syntax is below

   - name: Mount task 1 - create directory
     file:
       path: /dev/sdb1

         path: "{{ mount_point_of_dev_sdb1 }}"

Errata 2:

The code implicates a loop

   - name: Mount task 1 - create directory
     file:
       path: "{{ item.mount_path }}"
       state: directory
     loop: "{{ my_list_of_mountpoints }}"
     when: "item.mount_path not in mount.stdout_lines"

       when: "item.mount_device not in mount.stdout_lines"

Hi ,

That’s helpful, but is there an option to match partial string ?