---
- name: Open firewall for PGDG repository
  replace:
    name: /etc/default/minifirewall
    regexp: "^(HTTPSITES='((?!apt\\.postgresql\\.org).)*)'$"
    replace: "\\1 apt.postgresql.org'"
  notify: Restart minifirewall

- meta: flush_handlers

- name: Add PGDG repository
  apt_repository:
    repo: "deb http://apt.postgresql.org/pub/repos/apt/ {{ansible_distribution_release}}-pgdg main"
    update_cache: yes

- name: PGDG embedded GPG key is absent
  apt_key:
    id: "ACCC4CF8"
    keyring: /etc/apt/trusted.gpg
    state: absent

- name: Add PGDG GPG key
  copy:
    src: pgdg.asc
    dest: /etc/apt/trusted.gpg.d/pgdg.asc
    force: yes
    mode: "0644"
    owner: root
    group: root

- name: Update and upgrade apt packages for PGDG repository
  apt:
    upgrade: yes
    update_cache: yes

- name: Add APT preference file
  template:
    src: postgresql.pref.j2
    dest: /etc/apt/preferences.d/postgresql.pref
    mode: "0644"
