- name: install needed packages for server
  package: name={{ item }} state=present
  with_items:
  - glusterfs-server
  tags:
  - packages
  - gluster

- name: start glusterd for the server
  service: name=glusterd state=started enabled=true
  tags:
  - gluster

- name: Servers discover each other, pass one.
  command: gluster peer probe  {{ item }}
  with_items: '{{groups[gluster_server_group]}}'
  ignore_errors: true
  changed_when: false
  tags:
  - gluster

- name: Servers discover each other, pass two.
  command: gluster peer probe  {{ item }}
  with_items: '{{groups[gluster_server_group]}}'
  changed_when: false
  ignore_errors: true
  tags:
  - gluster

- name: install needed packages for client
  package: name={{ item }} state=present
  with_items:
  - glusterfs-cli
  - glusterfs-fuse
  - libselinux-python
  tags:
  - packages
  - gluster

- name: set sebooleans so httpd can talk to the gluster mount.
  seboolean: name={{ item }}
                    state=true
                    persistent=true
  with_items:
  - nis_enabled
  - httpd_use_fusefs
  tags:
  - gluster

- name: Ensure Gluster brick directories exist.
  file: "path={{ gluster_brick_dir }} state=directory mode=0775"
  when: inventory_hostname in groups[gluster_server_group]
  tags:
  - gluster

- name: Ensure Gluster mount directories exist.
  file: "path={{ gluster_mount_dir }} state=directory mode=0775"
  tags:
  - gluster

- name: Configure Gluster volume.
  gluster_volume:
    state: present
    name: "{{ gluster_brick_name }}"
    brick: "{{ gluster_brick_dir }}"
    # This cannot be '1'
    #replicas: "{{ groups[gluster_server_group] | count }}"
    cluster: "{{ groups[gluster_server_group] | join(',') }}"
    host: "{{ inventory_hostname }}"
    force: yes
  run_once: true
  ignore_errors: true
  tags:
  - gluster

- name: Ensure Gluster volume is mounted.
  mount:
    name: "{{ gluster_mount_dir }}"
    src: "{{ inventory_hostname }}:/{{ gluster_brick_name }}"
    fstype: glusterfs
    opts: "defaults,_netdev"
    state: mounted
  tags:
  - gluster
