lab-ansible/roles/postgresql/tasks/main.yml

99 lines
2.6 KiB
YAML
Raw Permalink Normal View History

2023-11-12 16:59:18 +00:00
# vim: ft=yaml.ansible
# code: language=ansible
---
- name: Add PostgreSQL repository
ansible.builtin.dnf:
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
state: present
- name: Disable built-in PostgreSQL module
2023-12-24 23:58:55 +00:00
ansible.builtin.command:
cmd: dnf -qy module disable postgresql
creates: /etc/dnf/modules.d/postgresql.module
2023-11-12 16:59:18 +00:00
- name: Install PostgreSQL {{ postgresql_version }}
ansible.builtin.dnf:
2023-11-12 19:22:32 +00:00
name:
- postgresql{{ postgresql_version }}-server
- python{{ ansible_python.version.major }}-psycopg2
update_cache: true
2023-11-12 16:59:18 +00:00
state: present
2023-11-12 19:22:32 +00:00
2023-12-27 23:07:21 +00:00
- name: Create PostgreSQL service override directory
2023-11-12 19:22:32 +00:00
ansible.builtin.file:
path: /etc/systemd/system/{{ postgresql_service }}.service.d
owner: root
2024-01-30 18:15:51 +00:00
group: root
2023-11-12 19:22:32 +00:00
mode: u=rwx,g=rx,o=rx
state: directory
- name: Copy PostgreSQL service override file
ansible.builtin.template:
2023-12-23 16:41:57 +00:00
src: "{{ postgresql_service }}.service.j2"
2023-11-12 19:22:32 +00:00
dest: /etc/systemd/system/{{ postgresql_service }}.service.d/override.conf
owner: root
2024-01-30 18:15:51 +00:00
group: root
2023-11-12 19:22:32 +00:00
mode: u=rw,g=r,o=r
notify: Reload systemd
- name: Flush handlers
ansible.builtin.meta: flush_handlers
2024-01-09 20:36:49 +00:00
- name: Create PostgreSQL directories
2023-11-13 20:17:17 +00:00
ansible.builtin.file:
2024-01-09 20:36:49 +00:00
path: "{{ item }}"
2023-12-27 23:07:21 +00:00
owner: postgres
2024-01-07 18:24:04 +00:00
group: postgres
2023-12-27 23:07:21 +00:00
mode: u=rwx,go=
2024-01-07 18:24:04 +00:00
seuser: system_u
serole: object_r
setype: postgresql_db_t
selevel: s0
2023-12-27 23:07:21 +00:00
state: directory
2024-01-09 20:36:49 +00:00
loop:
- "{{ postgresql_pgdata }}"
- "{{ postgresql_wal_archive }}"
2023-12-27 23:07:21 +00:00
2023-11-12 19:22:32 +00:00
- name: Initialize database
ansible.builtin.command:
cmd: /usr/pgsql-{{ postgresql_version }}/bin/postgresql-{{ postgresql_version }}-setup initdb
creates: "{{ postgresql_pgdata }}/base"
2023-11-12 19:22:32 +00:00
2023-12-23 16:41:57 +00:00
- name: Copy postgresql.conf
ansible.builtin.template:
src: postgresql.conf.j2
dest: "{{ postgresql_pgdata }}/postgresql.conf"
owner: postgres
2024-01-07 18:36:02 +00:00
group: postgres
2023-12-23 16:41:57 +00:00
mode: u=rw,go=
notify: Reload PostgreSQL
- name: Copy pg_hba.conf
ansible.builtin.template:
src: pg_hba.conf.j2
dest: "{{ postgresql_pgdata }}/pg_hba.conf"
owner: postgres
2024-01-07 18:36:02 +00:00
group: postgres
2023-12-23 16:41:57 +00:00
mode: u=rw,go=
notify: Reload PostgreSQL
2023-12-24 23:58:55 +00:00
- name: Flush handlers
ansible.builtin.meta: flush_handlers
2023-11-12 19:22:32 +00:00
- name: Ensure PostgreSQL is enabled and running
ansible.builtin.service:
name: "{{ postgresql_service }}"
enabled: true
state: started
2023-11-26 16:37:17 +00:00
- name: Set up databases
2023-12-24 23:58:55 +00:00
ansible.builtin.include_tasks:
file: database.yml
apply:
become: true
become_user: postgres
2024-03-02 21:36:05 +00:00
loop: "{{ databases | dict2items }}"
vars:
app: "{{ item.value }}"
2023-12-24 23:58:55 +00:00
no_log: true