---

- name: install EPEL repository
  yum: name=epel-release state=present
  when: >  # not for Fedora
    ansible_distribution == 'CentOS' or
    ansible_distribution == 'Red Hat Enterprise Linux'
  become: yes

- name: install python2 psycopg2
  yum:
    name:
      - python-psycopg2
      - sudo
      - wget
      - perl
    state: present

- name: install python3 psycopg2
  yum:
    name:
      - python3-psycopg2
      - sudo
      - wget
      - perl
    state: present

- name: fixup some locale issues
  lineinfile:
    dest: /etc/default/locale
    line: 'LANGUAGE="{{ item }}"'
    state: present
    create: yes
  loop:
    - 'en_US:en'
    - 'en_us.UTF-8'

- name: get latest version
  vars:
    base: http://download.postgresql.org/pub/repos/yum
    ver: "{{ ansible_distribution_version }}"
  shell: |
    set -eo pipefail
    wget -O - {{ base }}/{{ postgres_server_version }}/redhat/rhel-{{ ver }}-x86_64/ 2>/dev/null | \
    grep 'pgdg-redhat' | \
    perl -pe 's/^.*rpm">//g' | \
    perl -pe 's/<\/a>.*//g' | \
    tail -n 1
  args:
    executable: /bin/bash
  changed_when: false
  check_mode: false
  register: latest_version
  tags: [skip_ansible_lint]  # yes, I want wget here

- name: config postgres repository
  vars:
    base: http://download.postgresql.org/pub/repos/yum
    ver: "{{ ansible_distribution_version }}"
  yum:
    name: "{{ base }}/{{ postgres_server_version }}/redhat/rhel-{{ ver }}-x86_64/{{ latest_version.stdout }}"
    state: present
  become: yes

- name: install postgres packages
  yum:
    name:
      - postgresql{{ postgres_server_pkg_version }}-server
      - postgresql{{ postgres_server_pkg_version }}-contrib
      - postgresql{{ postgres_server_pkg_version }}-devel
    state: present
  become: yes
