Ansible when condition with ansible_facts

Hey All,

Trying to create a (when: ) condition, where a task runs only when a mount point does NOT exists.

  • name: format disk with ext4
    filesystem: fstype=ext4 dev={{ item }}
    with_items:
  • /dev/xvdc
    when: ansible_mounts.mount doesn’t contain “/path/to/my/mount/point”

The expectation is that ansible_mounts contains 1 or more mount points.

Any ideas?

Thanks!

ansible_mounts is a list of dictionaries, each of which has a 'mount'
so you are really comparing to a complex structure, you can try
something like this (though with set_fact to create the list you
need):

  vars:
    newdevices:
        - /dev/xvdc
        - /dev/sda1
  tasks:
    - debug: msg="mounted {{item.device}}"
      with_items: ansible_mounts
      when: item.device in newdevices