- name: set pg_cfg set_fact: pg_cfg: "{{ postgresql_default_config | d({}) | combine(postgresql_config | d({}), recursive=true) }}" - name: install dependencies include_tasks: tasks/install_packages.yml vars: package: - postgresql - postgresql-contrib - py3-psycopg2 - name: create user and group include_tasks: tasks/create_user.yml vars: user: name: "{{ postgresql_user }}" group: "{{ postgresql_group }}" - name: create config directory file: path: "{{ postgresql_conf_dir }}" state: directory mode: 0770 owner: "{{ postgresql_user }}" group: "{{ postgresql_group }}" - block: - name: create data directory file: path: "{{ postgresql_data_dir }}" state: directory mode: 0700 owner: "{{ postgresql_user }}" group: "{{ postgresql_group }}" rescue: - meta: noop - name: include custom config in default postgres config lineinfile: path: "{{ postgresql_conf_dir }}/postgresql.conf" line: "include 'custom.conf'" create: yes mode: 0400 owner: "{{ postgresql_user }}" group: "{{ postgresql_group }}" notify: restart postgresql - name: template custom config template: src: postgresql.j2 dest: "{{ postgresql_conf_dir }}/custom.conf" force: yes mode: 0400 owner: "{{ postgresql_user }}" group: "{{ postgresql_group }}" lstrip_blocks: yes notify: restart postgresql vars: config: "{{ pg_cfg }}" - name: template pg_hba.conf if it does not exist template: src: pg_hba.j2 dest: "{{ postgresql_conf_dir }}/pg_hba.conf" force: no mode: 0400 owner: "{{ postgresql_user }}" group: "{{ postgresql_group }}" notify: restart postgresql - name: ensure postgres hba allows local connections community.postgresql.postgresql_pg_hba: dest: "{{ postgresql_conf_dir }}/pg_hba.conf" contype: local databases: all users: all method: trust notify: restart postgresql - name: edit service config lineinfile: path: /etc/conf.d/postgresql regexp: "^{{ item.name }}=" line: '{{ item.name }}="{{ item.value }}"' notify: restart postgresql loop: - name: conf_dir value: "{{ postgresql_conf_dir }}" - name: data_dir value: "{{ postgresql_data_dir }}" - name: pg_opts value: -c log_destination='syslog' - name: initdb_opts value: --locale=en_US.UTF-8 - name: disable logfile in service config lineinfile: path: /etc/conf.d/postgresql regexp: '^logfile=(.*)$' line: '#logfile=\1' backrefs: yes notify: restart postgresql - name: collect .new files find: paths: "{{ postgresql_conf_dir }}" patterns: "*.new" register: new_files - name: remove .new files file: path: "{{ item.path }}" state: absent loop: "{{ new_files.files | flatten(levels=1) }}" - name: remove old postgres log file file: path: /var/log/postgresql/postmaster.log state: absent changed_when: no - name: flush handlers meta: flush_handlers # TODO: backup configuration for postgres - name: enable and start postgres service: name: postgresql state: started enabled: yes environment: LANG: 'en_US.UTF-8'