Skip to content

Commit

Permalink
Add base configuration facts
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak committed Mar 5, 2024
1 parent 14fe38c commit 134951f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ postgresql_service_state: started
postgresql_service_enabled: true
postgresql_db_init: true
postgresql_db_path: /var/lib/postgresql
postgresql_user: postgres
postgresql_group: postgres

_postgresql_package_name:
debian:
Expand All @@ -22,3 +24,8 @@ _postgresql_default_search:
- "{{ ansible_distribution | lower }}"
- "{{ ansible_os_family | lower }}"
- "default"
_postgresql_default_datadir: "/var/lib/postgresql/data"
_postgresql_default_config:
data_directory: "{{ _postgresql_default_datadir }}"
hba_file: "{{ _postgresql_default_datadir }}/pg_hba.conf"
ident_file: "{{ _postgresql_default_datadir }}/pg_ident.conf"
8 changes: 8 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
---
- name: Set installation facts
ansible.builtin.include_tasks: vars.yml

- name: Restart PostgreSQL
ansible.builtin.service:
name: "{{ _postgresql_service }}"
state: "restarted"
listen: postgresql_restart
32 changes: 32 additions & 0 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Set configuration facts
ansible.builtin.set_fact:
_postgresql_datadir: "{{ _postgresql_config.data_directory }}"
_postgresql_hba: "{{ _postgresql_config.hba_file }}"
_postgresql_ident: "{{ _postgresql_config.ident_file }}"

- name: Create PostgreSQL data directory
ansible.builtin.file:
path: "{{ _postgresql_datadir }}"
owner: "{{ postgresql_user }}"
group: "{{ postgresql_group }}"
state: directory
mode: "0700"

- name: Check if PostgreSQL database is initialized
ansible.builtin.stat:
path: "{{ _postgresql_datadir }}/PG_VERSION"
register: pgdatadir_stat

- name: Ensure PostgreSQL database is initialized

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / lint

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / molecule (install, ubuntu2204, /lib/systemd/systemd)

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / molecule (install, ubuntu2004, /lib/systemd/systemd)

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / molecule (install, debian12, /lib/systemd/systemd)

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / molecule (install, debian11, /lib/systemd/systemd)

no-changed-when

Commands should not change things if nothing needs doing.

Check failure on line 21 in tasks/configure.yml

View workflow job for this annotation

GitHub Actions / molecule (install, rockylinux9, /usr/lib/systemd/systemd)

no-changed-when

Commands should not change things if nothing needs doing.
ansible.builtin.command: "/usr/bin/initdb -D {{ _postgresql_datadir }}"
when: not pgdatadir_stat.stat.exists
become: true
become_user: "{{ postgresql_user }}"

- name: Set PostgreSQL environment variables
ansible.builtin.template:
src: postgres.sh.j2
dest: /etc/profile.d/postgres.sh
mode: "0644"
notify: Restart postgresql
6 changes: 6 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
- name: Set installation facts
ansible.builtin.include_tasks: vars.yml

- name: Verify installation parameters
ansible.builtin.include_tasks: verify.yml

- name: Install PostgreSQL services
ansible.builtin.include_tasks: install.yml

- name: Configure PostgreSQL services
ansible.builtin.include_tasks: configure.yml

- name: Manage PostgreSQL services
ansible.builtin.include_tasks: service.yml
3 changes: 2 additions & 1 deletion tasks/vars.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
- name: Set variable overrides
- name: Set OS variable overrides
ansible.builtin.set_fact:
_postgresql_package: "{{ postgresql_package_name | default(_default_package) }}"
_postgresql_service: "{{ postgresql_service_name | default(_default_service) }}"
_postgresql_config: "{{ postgresql_config | default(_postgresql_default_config) }}"
vars:
_default_search: "{{ _postgresql_default_search }}"
_default_package: "{{ _postgresql_package_name | nephelaiio.plugins.sorted_get(_default_search) }}"
Expand Down
11 changes: 11 additions & 0 deletions tasks/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Check datadir configuration
ansible.builtin.assert:
that:
- _hba_attr in _postgresql_config
- _postgresql_config[_datadir_attr] == _postgresql_datadir
fail_msg: postgresql_datadir attribute must match requested configuration
vars:
_hba_attr: hba_file
_datadir_attr: data_directory
_ident_attr: ident_file
1 change: 1 addition & 0 deletions templates/postgres.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export PGDATA={{ _postgresql_datadir }}

0 comments on commit 134951f

Please sign in to comment.