ansible/roles/postgresql/tasks/main.yml

82 lines
1.7 KiB
YAML
Raw Normal View History

2021-03-10 12:01:32 +01:00
---
- name: Install postgresql and psycopg2
2021-03-10 12:01:32 +01:00
apt:
update_cache: true
pkg:
- postgresql
- python3-psycopg2
2021-03-10 12:01:32 +01:00
state: present
register: apt_result
2021-03-10 12:08:05 +01:00
retries: 3
until: apt_result is succeeded
2021-03-10 12:01:32 +01:00
- name: Ensure main postgresql directory exists
file:
path: /etc/postgresql/{{ postgresql.version }}/main/
state: directory
owner: postgres
group: postgres
mode: 0755
- name: Ensure configuration directory exists
file:
path: /etc/postgresql/{{ postgresql.version }}/main/conf.d
state: directory
owner: postgres
group: postgres
mode: 0755
2021-03-10 12:11:02 +01:00
2021-03-10 12:01:32 +01:00
- name: Configuration of postgresql {{ postgresql.version }}
template:
src: postgresql/{{ item }}.j2
dest: /etc/postgresql/{{ postgresql.version }}/main/{{ item }}
mode: 0640
owner: postgres
group: postgres
loop:
- pg_hba.conf
2021-03-14 23:45:36 +01:00
- pg_ident.conf
2021-03-10 12:01:32 +01:00
- postgresql.conf
notify:
2021-03-14 23:45:36 +01:00
- Reload postgresql
- name: Enable and start PostgreSQL
systemd:
name: postgresql.service
state: started
enabled: true
- name: Create databases
become: true
become_user: postgres
postgresql_db:
name: "{{ item }}"
encoding: UTF-8
lc_collate: en_US.UTF-8
lc_ctype: en_US.UTF-8
template: template0
2021-11-28 10:45:21 +01:00
loop: "{{ postgresql.databases }}"
- name: Create users
become: true
become_user: postgres
postgresql_user:
db: "{{ item.database }}"
name: "{{ item.name }}"
password: "{{ item.password }}"
no_log: true
2021-11-28 10:45:21 +01:00
loop: "{{ postgresql.users }}"
2021-03-10 14:27:14 +01:00
- name: Grant privileges to users
become: true
become_user: postgres
postgresql_privs:
db: postgres
type: database
role: "{{ item.name }}"
privs: "{{ item.privs | join(',') }}"
obj: "{{ item.database }}"
2021-03-10 18:18:08 +01:00
no_log: true
2021-11-28 10:45:21 +01:00
loop: "{{ postgresql.users }}"
...