summaryrefslogtreecommitdiff
path: root/ansible-continuous-delivery/roles/install-postgres/tasks/main.yml
blob: 7a80045c6ddb1b1355fa096467e23cfbe1b67485 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
- name: Vérification de la présence des paramètres d'entrées dans les variables
  ansible.builtin.fail:
    msg: |
      La variable {{ item }} est obligatoire pour utiliser le role.
      Veuillez la renseigner dans le dossier host_vars.
  when:
    - item is not defined
  loop:
    - db_user
    - db_password
    - db_name

- name: Apt Update and Install Postgres
  ansible.builtin.apt:
    update_cache: true
    name:
      - postgresql
      - acl
      - python3-psycopg2
    state: present
  when: ansible_pkg_mgr == "apt"

- name: Yum Update and Install Postgres
  ansible.builtin.yum:
    update_cache: true
    name:
      - postgresql
      - postgresql-server
      - postgresql-contrib
      - acl
      - python3-psycopg2
    state: present
  when: ansible_pkg_mgr == "yum"

- name: Dnf Update and Install Postgres
  ansible.builtin.dnf:
    update_cache: true
    name:
      - postgresql
      - postgresql-server
      - postgresql-contrib
      - acl
      - python3-psycopg2
    state: present
  when: ansible_pkg_mgr == "dnf"

- name: Yum/Dnf management
  when: ansible_pkg_mgr == "dnf" or ansible_pkg_mgr == "yum"
  block:
    - name: Find out if PostgreSQL is initialized
      ansible.builtin.stat:
        path: "/var/lib/pgsql/data/pg_hba.conf"
      register: postgres_data

    - name: Init database only for Yum/Dnf OS
      ansible.builtin.command: /usr/bin/postgresql-setup --initdb
      when: not postgres_data.stat.exists

- name: Start and enable services
  ansible.builtin.service:
    name: postgresql
    state: started
    enabled: true

- name: Create db user
  community.postgresql.postgresql_user:
    state: present
    name: "{{ db_user }}"
    password: "{{ db_password }}"
    role_attr_flags: CREATEDB
  become: true
  become_user: postgres

- name: Allow md5 connection for the db user
  community.postgresql.postgresql_pg_hba:
    dest: "/etc/postgresql/12/main/pg_hba.conf"
    contype: host
    databases: all
    method: md5
    users: "{{ db_user }}"
    create: true
  become: true
  become_user: postgres
  notify: Restart postgres
  when: ansible_pkg_mgr == "apt"

- name: Allow md5 connection for the db user
  community.postgresql.postgresql_pg_hba:
    dest: "~/data/pg_hba.conf"
    contype: host
    databases: all
    method: md5
    users: "{{ db_user }}"
    create: true
  become: true
  become_user: postgres
  notify: Restart postgres
  when: ansible_pkg_mgr == "dnf" or ansible_pkg_mgr == "yum"