diff --git a/roles/postgresql/defaults/main.yml b/roles/postgresql/defaults/main.yml index 6c93d02..27979b8 100644 --- a/roles/postgresql/defaults/main.yml +++ b/roles/postgresql/defaults/main.yml @@ -2,5 +2,6 @@ # code: language=ansible --- postgresql_version: 14 -postgresql_pgdata: /var/lib/pgsql/{{ postgresql_version }}/data +postgresql_pgdata_default: /var/lib/pgsql/{{ postgresql_version }}/data +postgresql_pgdata: "{{ postgresql_pgdata_default }}" postgresql_service: postgresql-{{ postgresql_version }} diff --git a/roles/postgresql/handlers/main.yml b/roles/postgresql/handlers/main.yml new file mode 100644 index 0000000..ace34e1 --- /dev/null +++ b/roles/postgresql/handlers/main.yml @@ -0,0 +1,6 @@ +# vim: ft=yaml.ansible +# code: language=ansible +--- +- name: Reload systemd + ansible.builtin.systemd: + daemon_reload: true diff --git a/roles/postgresql/tasks/main.yml b/roles/postgresql/tasks/main.yml index 604afdf..ec527ad 100644 --- a/roles/postgresql/tasks/main.yml +++ b/roles/postgresql/tasks/main.yml @@ -18,5 +18,43 @@ - name: Install PostgreSQL {{ postgresql_version }} ansible.builtin.dnf: - name: postgresql{{ postgresql_version }}-server + name: + - postgresql{{ postgresql_version }}-server + - python{{ ansible_python.version.major }}-psycopg2 state: present + +- name: Create PostgreSQL service override directory + ansible.builtin.file: + path: /etc/systemd/system/{{ postgresql_service }}.service.d + owner: root + mode: u=rwx,g=rx,o=rx + state: directory + +- name: Copy PostgreSQL service override file + ansible.builtin.template: + src: systemd-overrides/{{ postgresql_service }}.service.j2 + dest: /etc/systemd/system/{{ postgresql_service }}.service.d/override.conf + owner: root + mode: u=rw,g=r,o=r + when: postgresql_pgdata != postgresql_pgdata_default + notify: Reload systemd + +- name: Flush handlers + ansible.builtin.meta: flush_handlers + +- name: Check if pgdata directory is initialized + ansible.builtin.stat: + path: "{{ postgresql_pgdata }}/base" + register: pgdata_stat + failed_when: false + +- name: Initialize database + ansible.builtin.command: + cmd: /usr/pgsql-{{ postgresql_version }}/bin/postgresql-{{ postgresql_version }}-setup initdb + when: pgdata_stat.stat.isdir is not defined or not pgdata_stat.stat.isdir + +- name: Ensure PostgreSQL is enabled and running + ansible.builtin.service: + name: "{{ postgresql_service }}" + enabled: true + state: started diff --git a/roles/postgresql/templates/systemd-overrides/postgresql-14.service.j2 b/roles/postgresql/templates/systemd-overrides/postgresql-14.service.j2 new file mode 100644 index 0000000..160de22 --- /dev/null +++ b/roles/postgresql/templates/systemd-overrides/postgresql-14.service.j2 @@ -0,0 +1,3 @@ +# code: language=ansible-jinja +[Service] +Environment=PGDATA={{ postgresql_pgdata }} \ No newline at end of file