From 134951f0cc83bc8249fc5133dd34887e5865d8ca Mon Sep 17 00:00:00 2001 From: Ted Cook Date: Tue, 5 Mar 2024 17:49:10 -0600 Subject: [PATCH] Add base configuration facts --- defaults/main.yml | 7 +++++++ handlers/main.yml | 8 ++++++++ tasks/configure.yml | 32 ++++++++++++++++++++++++++++++++ tasks/main.yml | 6 ++++++ tasks/vars.yml | 3 ++- tasks/verify.yml | 11 +++++++++++ templates/postgres.sh.j2 | 1 + 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tasks/configure.yml create mode 100644 tasks/verify.yml create mode 100644 templates/postgres.sh.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 5f6947a..19df8f1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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: @@ -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" diff --git a/handlers/main.yml b/handlers/main.yml index ed97d53..e52972b 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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 diff --git a/tasks/configure.yml b/tasks/configure.yml new file mode 100644 index 0000000..13612ca --- /dev/null +++ b/tasks/configure.yml @@ -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 + 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 diff --git a/tasks/main.yml b/tasks/main.yml index 400a3a1..26283a6 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/tasks/vars.yml b/tasks/vars.yml index fa1de36..47d831d 100644 --- a/tasks/vars.yml +++ b/tasks/vars.yml @@ -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) }}" diff --git a/tasks/verify.yml b/tasks/verify.yml new file mode 100644 index 0000000..8141768 --- /dev/null +++ b/tasks/verify.yml @@ -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 diff --git a/templates/postgres.sh.j2 b/templates/postgres.sh.j2 new file mode 100644 index 0000000..3c2f26e --- /dev/null +++ b/templates/postgres.sh.j2 @@ -0,0 +1 @@ +export PGDATA={{ _postgresql_datadir }}