# file: postgresql/tasks/configure.yml - name: PostgreSQL | Drop the data directory | RedHat file: path: "{{ postgresql_data_directory }}" state: absent register: pgdata_dir_remove when: ansible_os_family == "RedHat" and postgresql_cluster_reset - name: PostgreSQL | Make sure the postgres data directory exists file: path: "{{postgresql_data_directory}}" owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" state: directory mode: 0700 register: pgdata_dir_exist - name: PostgreSQL | Ensure the locale for lc_collate and lc_ctype is generated | Debian become: yes locale_gen: name="{{ item }}" state=present with_items: - "{{ postgresql_locale }}" - "{{ postgresql_ctype }}" when: ansible_os_family == "Debian" - name: PostgreSQL | Ensure the locale is generated | RedHat become: yes command: localedef -c -i {{ item.parts[0] }} -f {{ item.parts[1] }} {{ item.locale_name }} changed_when: false with_items: - { parts: "{{ postgresql_locale_parts }}", locale_name: "{{ postgresql_locale }}" } - { parts: "{{ postgresql_ctype_parts }}", locale_name: "{{ postgresql_ctype }}" } when: ansible_os_family == "RedHat" - name: PostgreSQL | Reset the cluster - drop the existing one | Debian shell: pg_dropcluster --stop {{ postgresql_version }} {{ postgresql_cluster_name }} become: yes become_user: "{{ postgresql_service_user }}" when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed - name: PostgreSQL | Reset the cluster - create a new one (with specified encoding and locale) | Debian shell: > pg_createcluster --start --locale {{ postgresql_locale }} -e {{ postgresql_encoding }} -d {{ postgresql_data_directory }} {{ postgresql_version }} {{ postgresql_cluster_name }} become: yes become_user: "{{ postgresql_service_user }}" when: ansible_os_family == "Debian" and postgresql_cluster_reset and pgdata_dir_exist.changed - name: PostgreSQL | Check whether the postgres data directory is initialized stat: path: "{{ postgresql_data_directory }}/PG_VERSION" when: ansible_os_family == "RedHat" and not postgresql_cluster_reset register: pgdata_dir_initialized - name: PostgreSQL | Initialize the database | RedHat command: > {{ postgresql_bin_directory }}/initdb -D {{ postgresql_data_directory }} --locale={{ postgresql_locale }} --encoding={{ postgresql_encoding }} become: yes become_user: "{{ postgresql_service_user }}" when: ansible_os_family == "RedHat" and (postgresql_cluster_reset or pgdata_dir_exist.changed or not pgdata_dir_initialized.stat.exists) - name: PostgreSQL | Ensure configuration directory exists file: path: "{{ postgresql_conf_directory }}" owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" mode: 0750 state: directory - name: PostgreSQL | Update configuration - pt. 1 (pg_hba.conf) template: src: pg_hba.conf.j2 dest: "{{postgresql_conf_directory}}/pg_hba.conf" owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" mode: 0640 register: postgresql_configuration_pt1 - name: PostgreSQL | Update configuration - pt. 2 (postgresql.conf) template: src: "postgresql.conf-{{ postgresql_version }}.j2" # if using pgtune, save the template to ".untuned" dest: "{{postgresql_conf_directory}}/postgresql.conf{% if postgresql_pgtune %}.untuned{% endif %}" owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" mode: 0640 register: postgresql_configuration_pt2 - name: PostgreSQL | Update configuration - pt. 3 (pgtune) become: true become_user: "{{ postgresql_service_user }}" shell: | set -e TMPDIR=$(mktemp -d) pgtune --input-config=postgresql.conf.untuned\ --output-config=$TMPDIR/postgresql.conf\ {% if postgresql_pgtune_memory %}--memory {{postgresql_pgtune_memory|int}}{% endif %}\ --type {{postgresql_pgtune_type|quote}}\ {% if postgresql_pgtune_connections %}--connections {{postgresql_pgtune_connections|int}}{% endif %}\ # Compare the current config with the one procuded by pgtune (ignoring comments/blanks) if diff --ignore-blank-lines \ <(sed -e 's/#.*//' postgresql.conf) \ <(sed -e 's/#.*//' $TMPDIR/postgresql.conf) then echo '_OK_' else cp $TMPDIR/postgresql.conf postgresql.conf fi # Cleanup rm $TMPDIR/postgresql.conf rmdir $TMPDIR args: chdir: "{{postgresql_conf_directory}}" executable: "/bin/bash" when: postgresql_pgtune register: postgresql_configuration_pt3 changed_when: "'_OK_' not in postgresql_configuration_pt3.stdout" - name: PostgreSQL | Create folder for additional configuration files file: name: "{{postgresql_conf_directory}}/conf.d" state: directory owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" mode: 0755 - name: PostgreSQL | Ensure the systemd directory for PostgreSQL exists | RedHat file: name: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d" state: directory mode: 0755 when: ansible_os_family == "RedHat" - name: PostgreSQL | Use the conf directory when starting the Postgres service | RedHat template: src: etc_systemd_system_postgresql.service.d_custom.conf.j2 dest: "/etc/systemd/system/postgresql-{{ postgresql_version }}.service.d/custom.conf" when: ansible_os_family == "RedHat" register: postgresql_systemd_custom_conf - name: PostgreSQL | Ensure the pid directory for PostgreSQL exists file: name: "{{ postgresql_pid_directory }}" state: directory owner: "{{ postgresql_service_user }}" group: "{{ postgresql_service_group }}" mode: u=rwX,g=rwXs,o=rx - name: PostgreSQL | Enable service service: name: "{{ postgresql_service_name }}" enabled: yes when: "{{ postgresql_service_enabled | bool }}" - name: PostgreSQL | Restart PostgreSQL service: name: "{{ postgresql_service_name }}" state: restarted when: postgresql_configuration_pt1.changed or postgresql_configuration_pt2.changed or postgresql_configuration_pt3.changed or postgresql_systemd_custom_conf.changed