aboutsummaryrefslogblamecommitdiffstats
path: root/roles/postgres/tasks/main.yml
blob: 16fce30e33a9365aa26ffd38f4e846ac496d3a90 (plain) (tree)
1
2
3
4
5
6
7
8
9
10





                                           
                                                


                       
                                         


                          
 


                                        











                                                  




                                          
       
                                         

                                 
                
                    


            







                                              

                 

                                         
                                                                        
                              

                                                                                                                                                



                                                                        

                               
                  
                                                                                                             





                                           

# postgres ~~ roles/postgres/tasks/main.yml
# postgresql database server

---

- name: ensure postgres is installed for OpenBSD
  package:
    name: "{{ item }}" 
    state: present
  when: ansible_distribution == "OpenBSD"
  loop:
    - "postgresql-server"
    - "postgresql-contrib"

- name: ensure postgres is installed
  package: name=postgresql state=present
  ignore_errors: true
  when: ansible_distribution != "OpenBSD"

- name: ensure postgres group exists
  group: name="{{ postgres_group }}" state=present

- name: ensure postgres user exists
  user: 
    name: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    system: true
    state: present

- name: delete default data dir if exists
  file: path=/var/postgresql state=absent
  when: postgres_dir != "/var/postgresql"

- name: ensure postgres directories exists
  file:
    path: "{{ postgres_dir }}/{{ item }}"
    owner: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    mode: "0750"
    state: directory
  loop:
    - ""
    - "data"

- name: link postgres directory to default one
  file:
    src: "{{ postgres_dir }}"
    dest: "/var/postgresql"
    owner: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    state: link
    mode: "0750"
    follow: false
  when: postgres_dir != "/var/postgresql"

  # NOTE: this tasks needs `pipelining` set to `false` to work correctly
- name: init postgres database
  shell: |
    initdb -D "{{ postgres_dir }}/data/" -U "{{ postgres_dbuser }}" --encoding=UTF-8 --locale=en_US.UTF-8 {# --auth=scram-sha-256 --pwprompt #} 
    su - _postgresql -c \
      'initdb -D "{{ postgres_dir }}/data/" -U "{{ postgres_dbuser }}" \
      --encoding=UTF-8 \
      --locale=en_US.UTF-8'
  args:
    chdir: "{{ postgres_dir }}"
  register: result
  failed_when: result.rc != 0 and result.stderr is defined and "exists but is not empty" not in result.stderr

- name: enable and restart postgres service
  service:
    name: postgresql
    state: restarted
    enabled: true
remember that computers suck.