# platform = multi_platform_all
# reboot = false
# strategy = configure
# complexity = low
# disruption = high

{{% if MOUNT_HAS_TO_EXIST == "no" %}}
   {{% set TABFILE="" %}}
{{% else %}}
   {{% set TABFILE="--fstab" %}}
{{% endif %}}

- name: Check information associated to mountpoint
  command: findmnt {{{ TABFILE }}} '{{{ MOUNTPOINT }}}'
  register: device_name
  failed_when: device_name.rc > 1
  changed_when: False

- name: Create mount_info dictionary variable
  set_fact:
    mount_info: "{{ mount_info|default({})|combine({item.0: item.1}) }}"
  with_together:
    - "{{ device_name.stdout_lines[0].split() | list | lower }}"
    - "{{ device_name.stdout_lines[1].split() | list }}"
  when:
    - device_name.stdout is defined and device_name.stdout_lines is defined
    - (device_name.stdout | length > 0)

- name: Make sure {{{ MOUNTOPTION }}} option is part of the to {{{ MOUNTPOINT }}} options
  set_fact:
    mount_info: "{{ mount_info | combine( {'options':''~mount_info.options~',{{{ MOUNTOPTION }}}' }) }}"
  when:
    - mount_info is defined and "{{{ MOUNTOPTION }}}" not in mount_info.options

- name: Ensure {{{ MOUNTPOINT }}} is mounted with {{{ MOUNTOPTION }}} option
  mount:
    path: "{{{ MOUNTPOINT }}}"
    src: "{{ mount_info.source }}"
    opts: "{{ mount_info.options }}"
    state: "mounted"
    fstype: "{{ mount_info.fstype }}"
  when:
    - device_name.stdout is defined
    - (device_name.stdout | length > 0)
