---
  # "Install the PostgreSQL database."
  
  - include: locale_install.yml
    
  - name: "Add the PostgreSQL packages."
    apt:
       name: "{{ item }}"
       state: present
    register: ins_result
    with_items:
       - postgresql
       - "postgresql-client-{{ pg_version }}"
       - postgresql-common
       - postgresql-contrib
       - "postgresql-server-dev-{{ pg_version }}"
          
  - name: "Create the data directory." 
    file:
       path: "{{ data_dir }}"
       state: directory
       owner: "postgres"
       group: "postgres"
       
  - name: "Check if the 'main' cluster exists."
    shell: pg_lsclusters -h | cut -f2 -d" " | grep main
    register: main_exists
    ignore_errors: true
    changed_when: false
       
  - name: "Drop the default cluster."  
    shell: pg_dropcluster --stop "{{ pg_version }}" main
    become_method: sudo
    become_user: postgres
    when: main_exists.rc == 0
      
  - name: "Check if the 'fwd' cluster exists."
    shell: pg_lsclusters -h | cut -f2 -d" " | grep "{{ cluster_name }}"
    register: cluster_exists
    ignore_errors: true
    changed_when: false
    
  - name: "Create the 'fwd' cluster."  
    shell: pg_createcluster "{{ pg_version }}" "{{ cluster_name }}" "--locale=en_US@p2j_basic" -D "{{ data_dir }}/{{ pg_version }}/"
    become_method: sudo
    become_user: postgres
    register: create_result
    when: cluster_exists.rc == 1
      
  - name: "Modify the pg_hba.conf to limit access to the fwd user group."
    replace:
       dest: "/etc/postgresql/{{ pg_version }}/{{ cluster_name }}/pg_hba.conf"
       regexp: 'host\s+all\s+all\s+127.0.0.1'
       replace: 'host    all             +{{ db_user_id }}    127.0.0.1'
       backup: yes
    register: access_result
    when: create_result.changed and create_result|success      
      
  # in ansible 2.2 this can be done with the systemd module
  - name: "Issue daemon-reload to pick up config changes."
    shell: systemctl daemon-reload
    register: reload_result
    when: access_result.changed and access_result|success
  - name: "Start the cluster."
    shell: pg_ctlcluster "{{ pg_version }}" "{{ cluster_name }}" start
    register: start_result
    when: reload_result.changed and reload_result|success
      
  - name: "Create the fwd cluster application server user."
    shell: createuser "--cluster={{ pg_version }}/{{ cluster_name }}" -S -D -R "{{ db_user_id }}"
    become_method: sudo
    become_user: postgres
    register: create_user_result
    when: start_result.changed and start_result|success
      
  - name: "Create the fwd cluster admin user."
    shell: createuser "--cluster={{ pg_version }}/{{ cluster_name }}" -s -g "{{ db_user_id }}" "{{ db_adm_id }}"
    become_method: sudo
    become_user: postgres
    register: create_adm_result
    when: create_user_result.changed and create_user_result|success
  
  - name: "Set the password for the fwd cluster users."
    shell: psql -p "{{ db_port }}" -c "alter role {{ item.userid }} with encrypted password '{{ item.pw }}';"
    with_items:
       - { userid: "{{ db_user_id }}", pw: "{{ db_user_pw }}" }
       - { userid: "{{ db_adm_id }}", pw: "{{ db_adm_pw }}" }
    become_method: sudo
    become_user: postgres
    when: create_adm_result.changed and create_adm_result|success
  
  - include: pljava_install.yml