aboutsummaryrefslogtreecommitdiffstats
path: root/roles/postgres/tasks/main.yml
blob: 2413b03afd831127bacc08ee8aa0fa2bc89861e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

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

---

- name: ensure postgres is installed for OpenBSD
  package: name=postgresql-server state=present
  when: ansible_distribution == "OpenBSD"

- 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: ensure postgres directory exists
  file:
    path: "{{ postgres_dir }}"
    owner: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    mode: 0760
    state: directory

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

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

- name: init postgres database
  shell: initdb -D "{{ postgres_dir }}/data/" -U postgres --auth=scram-sha-256 --pwprompt --encoding=UTF-8 --locale=xx_XX.UTF-8
  args:
    chdir: "{{ postgres_dir }}"
  become: true
  become_user: "{{ postgres_user }}"
  register: result
  failed_when: result.rc != 0 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.