---
- name: Create PostgreSQL super user
  become_user: postgres
  postgresql_user: name="{{ postgresql_username }}"
                   password="{{ postgresql_password }}"
                   role_attr_flags=SUPERUSER
                   state=present

- name: Create PostgreSQL database
  become_user: postgres
  postgresql_db: name="{{ postgresql_database }}"
                 owner="{{ postgresql_username }}"

- name: Add PostGIS extension
  become_user: postgres
  command: psql {{ postgresql_database }} -c "CREATE EXTENSION postgis"
  register: psql_result
  failed_when: >
    psql_result.rc != 0 and ("already exists" not in psql_result.stderr)
  changed_when: "psql_result.rc == 0"

---
- name: Install ACL, required for Ansible Super User
  apt:
    name: acl
    state: present

- name: Create PostgreSQL super user
  become_user: postgres
  postgresql_user: name="{{ postgresql_username }}"
                   password="{{ postgresql_password }}"
                   role_attr_flags=SUPERUSER
                   state=present

- name: Create PostgreSQL database
  become_user: postgres
  postgresql_db: name="{{ postgresql_database }}"
                 owner="{{ postgresql_username }}"

- name: Add PostGIS extension
  become_user: postgres
  command: psql {{ postgresql_database }} -c "CREATE EXTENSION postgis"
  register: psql_result
  failed_when: >
    psql_result.rc != 0 and ("already exists" not in psql_result.stderr)
  changed_when: "psql_result.rc == 0"

Install acl package for PostgreSQL installation
Install acl package for PostgreSQL installation

Previously this package was preinstalled in Ubuntu. As of Focal
it is no longer there. It is needed for Ansible to use a
non-super-user role, so we install it explicitly.

See https://github.com/georchestra/ansible/issues/55#issuecomment-651043423
