ansible-role-postgresql/tasks/main.yml

101 lines
2.7 KiB
YAML
Raw Normal View History

2021-09-10 12:09:38 +00:00
---
2023-08-28 13:01:36 +00:00
- name: Install depencies packages
2021-09-10 12:09:38 +00:00
ansible.builtin.package:
name:
- gpg
- python3-psycopg2
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Import repo tasks
ansible.builtin.import_tasks: repo.yml
2021-09-10 12:09:38 +00:00
when: postgresql_use_official_repository
2023-08-28 13:01:36 +00:00
- name: Install packages
2021-09-10 12:09:38 +00:00
ansible.builtin.package:
name:
2023-08-28 13:01:36 +00:00
- "postgresql-{{ postgresql_version }}"
2021-09-10 12:09:38 +00:00
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Copy config
2021-09-10 12:09:38 +00:00
ansible.builtin.template:
2023-08-28 13:01:36 +00:00
src: "{{ item }}.j2"
dest: "/etc/postgresql/{{ postgresql_version }}/main/{{ item }}"
2021-09-10 12:09:38 +00:00
owner: root
group: root
2023-08-28 13:01:36 +00:00
mode: "0644"
2021-09-10 12:09:38 +00:00
loop:
- postgresql.conf
- pg_hba.conf
2023-08-28 13:01:36 +00:00
notify: Reload postgresql
2021-09-10 12:09:38 +00:00
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Enable and start service
2021-09-10 12:09:38 +00:00
ansible.builtin.service:
2023-08-28 13:01:36 +00:00
name: "postgresql@{{ postgresql_version }}-main"
2021-09-10 12:09:38 +00:00
state: started
enabled: true
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Wait for service is up
2021-09-10 12:09:38 +00:00
ansible.builtin.wait_for:
port: 5432
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Manage users
2021-09-10 12:09:38 +00:00
community.general.postgresql_user:
2023-08-28 13:01:36 +00:00
name: "{{ item.key }}"
password: "{{ item.value.password }}"
role_attr_flags: "{{ item.value.attributes | default([]) | join(',') }}"
state: "{{ item.value.state | default('present') }}"
loop: "{{ postgresql_users | dict2items }}"
loop_control:
2023-08-28 13:01:36 +00:00
label: "{{ item.key }}"
become: true
become_user: postgres
when: postgresql_primary
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Manage databases
2022-07-20 08:35:39 +00:00
community.general.postgresql_db:
2023-08-28 13:01:36 +00:00
name: "{{ item.name }}"
encoding: "{{ item.encoding | default('UTF-8') }}"
state: "{{ item.state | default('present') }}"
lc_collate: "{{ item.lc_collate | default('C.UTF-8') }}"
lc_ctype: "{{ item.lc_ctype | default('C.UTF-8') }}"
template: "{{ item.template | default('template0') }}"
owner: "{{ item.owner }}"
loop: "{{ postgresql_databases }}"
2022-07-20 08:35:39 +00:00
become: true
become_user: postgres
when: postgresql_primary
tags: postgresql
2023-08-28 13:01:36 +00:00
- name: Manage user"s privileges
community.postgresql.postgresql_privs:
2023-08-28 13:01:36 +00:00
db: "{{ item.database }}"
roles: "{{ item.role }}"
type: "{{ item.type | default('table') }}"
objs: "{{ item.objets | default(['ALL_IN_SCHEMA']) | join(',') }}"
privs: "{{ item.privileges | join(',') }}"
state: "{{ item.state | default('present') }}"
loop: "{{ postgresql_privileges }}"
2021-09-10 12:09:38 +00:00
loop_control:
2023-08-28 13:01:36 +00:00
label: "{{ item.role }}/{{ item.database }}"
2021-09-10 12:09:38 +00:00
become: true
become_user: postgres
2021-09-11 12:38:26 +00:00
when: postgresql_primary
2021-09-10 12:09:38 +00:00
tags: postgresql
2022-05-06 12:13:39 +00:00
2023-08-28 13:01:36 +00:00
- name: Manage role membership
2022-05-06 12:13:39 +00:00
community.postgresql.postgresql_membership:
2023-08-28 13:01:36 +00:00
groups: "{{ item.value.groups }}"
target_roles: "{{ item.key }}"
state: "{{ item.value.state | default('present') }}"
loop: "{{ postgresql_role_memberships | dict2items }}"
2022-05-06 12:13:39 +00:00
loop_control:
2023-08-28 13:01:36 +00:00
label: "{{ item.key }}"
2022-05-06 12:13:39 +00:00
become: true
become_user: postgres
when: postgresql_primary
tags: postgresql