- name: Get Quobyte Client Service Status
  service_facts:

- name: Convert From /etc/fstab
  become: true
  lineinfile:
    path: /etc/fstab
    regexp: '.*{{ quobyte_mount_point }}.*'
    state: absent
  register: quobyte_fstab

- name: Unmount fstab Entry
  become: true
  mount:
    path: "{{ quobyte_mount_point }}"
    state: unmounted
  when:
    - quobyte_fstab.changed
    - ('quobyte-client' not in ansible_facts.services) or (ansible_facts.services['quobyte-client'].state != 'running')

- name: Stat {{ quobyte_mount_point }}
  stat:
    path: "{{ quobyte_mount_point }}"
  register: tcs_mount
  failed_when: false
  changed_when: (tcs_mount.msg | default('')) == "Transport endpoint is not connected"

- name: Remount Broken {{ quobyte_mount_point }}
  become: true
  service:
    name: quobyte-client
    state: restarted
  when: (tcs_mount.msg | default('')) == "Transport endpoint is not connected"
  register: quobyte_systemd_remounted

- name: "{{ quobyte_mount_point }} Mount Point"
  become: true
  file:
    path: "{{ quobyte_mount_point }}"
    state: directory

- name: Quobyte Client Service Config Files
  become: true
  template:
    dest: /{{ item }}
    src: "{{ item }}.j2"
    mode: "0600"
  loop:
    - etc/quobyte/client-service.cfg
    - etc/quobyte/client-service.env
  notify: Remount Quobyte

- name: Make Sure Quobyte Service is Available
  become: true
  systemd:
    daemon_reload: true
  when: "'quobyte-client' not in ansible_facts.services"

- name: Quobyte Client Service
  become: true
  service:
    name: quobyte-client
    state: started
    enabled: true
  register: quobyte_systemd_remounted
