- include_tasks: "prep_os/{{ ansible_os_family }}.yml"

### Configuration

- name: Create gluster share directory
  file:
    path: "{{ item }}"
    state: directory
    owner: root
    group: root
    mode: '0750'
  with_items: ["/mnt/data", "/nextcloud"]
  changed_when: false

- name: Show all the gluster_servers in the inventory
  debug:
    msg: "{{ groups['gluster_servers'] | list }}"

- name: Create gluster 1
  gluster_volume:
    state: present
    force: yes
    name: ncgluster1
    bricks: /mnt/data/g1
    #rebalance: yes
    host: "{{ inventory_hostname }}"
    replicas: 2
    cluster: "{{ groups['gluster_servers'] | list }}"
  run_once: true
  ignore_errors: yes

- name: Create gluster 2
  gluster_volume:
    state: present
    force: yes
    name: ncgluster2
    bricks: /nextcloud/g1
    host: "{{ inventory_hostname }}"
    replicas: 2
    cluster: "{{ groups['gluster_servers'] | list }}"
  run_once: true
  ignore_errors: yes

- name: Set tuning quorum-count on GlusterFS volumes
  shell: "gluster volume set {{ item }}"
  loop:
    - "ncgluster1 cluster.quorum-count 1"
    - "ncgluster1 cluster.quorum-reads false"
    - "ncgluster2 cluster.quorum-count 1"
    - "ncgluster2 cluster.quorum-reads false"
  loop_control:
    pause: 2
  run_once: true
  delegate_to: "{{ groups['gluster_servers'][0] }}"

- name: Heal GlusterFS volumes
  shell: "gluster volume heal {{ item }} enable"
  loop:
    - ncgluster1
    - ncgluster2
  loop_control:
    pause: 2
  run_once: true
  delegate_to: "{{ groups['gluster_servers'][0] }}"

#- name: start gluster volume
#  gluster_volume:
#    state: started
#    name: ncgluster

- name: GlusterFS reStarting
  service:
    name: glusterd
    state: restarted
