---
- name: Update apt cache
  apt: update_cache=yes cache_valid_time=604800

- name: Install PostgreSQL
  become: yes
  apt: name={{ item }} state=present
  with_items:
    - libpq-dev
    - postgresql
    - python-psycopg2

- name: Create the database
  become: yes
  become_user: postgres
  postgresql_db: name={{ postgres.db }} encoding='UTF8' state=present

- name: Create the database user
  become: yes
  become_user: postgres
  postgresql_user: >
    db={{ postgres.db }}
    name={{ postgres.user }}
    password={{ postgres.password }}
    role_attr_flags=NOSUPERUSER,NOCREATEDB
    state=present

---
- name: Update apt cache
  become: yes
  apt: update_cache=yes cache_valid_time=604800

- name: Install PostgreSQL
  become: yes
  apt: name={{ item }} state=present
  with_items:
    - libpq-dev
    - postgresql
    - python-psycopg2

- name: Create the database
  become: yes
  become_user: postgres
  postgresql_db: name={{ postgres.db }} encoding='UTF8' state=present

- name: Create the database user
  become: yes
  become_user: postgres
  postgresql_user: >
    db={{ postgres.db }}
    name={{ postgres.user }}
    password={{ postgres.password }}
    role_attr_flags=NOSUPERUSER,NOCREATEDB
    state=present

Update the cache with sudo.
Update the cache with sudo.
