--- - name: install postgresql ansible.builtin.package: name: - postgresql-server - postgresql-contrib state: present - name: install python libraries for ansible ansible.builtin.package: name: py3-psycopg2 state: present - name: create postgresql data directory ansible.builtin.file: path: "{{ postgresql__data_dir }}" owner: "{{ postgresql__user }}" group: "{{ postgresql__group }}" mode: "0700" state: directory - name: init postgresql database # noqa: no-changed-when ansible.builtin.command: argv: - initdb - "--pgdata={{ postgresql__data_dir }}" - "--username={{ postgresql__db_user }}" - --encoding=UTF-8 - --locale=en_US.UTF-8 creates: "{{ postgresql__data_dir }}/PG_VERSION" become: true become_user: "{{ postgresql__user }}" - name: enable and restart postgresql service ansible.builtin.service: name: postgresql state: restarted enabled: true