Ansible is so great and simple tool that even I can use it! 🙂

Here is a very simple playbook to install PostgreSQL of desired version on Ubuntu 14+:

---

# this should work for any latest versions of Ubuntu and PostgreSQL
# although tested only with Ubuntu 14 and Pg 9.5

- name: install PostgreSQL on Ubuntu 
  hosts: local-test2
  sudo: yes

  vars:
    pg_version: "{{ pgversion }}"

  tasks:

  - fail: msg="You must supply pg version to install like -e pgversion=9.5"
    when: pg_version is not defined

  - name: find ubuntu main repository name
    command: "lsb_release -cs"
    register: ubuntu_release

  - debug: var=ubuntu_release.stdout
   
  - name: check if repository is added
    apt_repository:
      repo: "deb http://apt.postgresql.org/pub/repos/apt/ {{ubuntu_release.stdout}}-pgdg main"
      state: present

  - name: import repository keys     
    apt_key:
      url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
      state: present

  - name: update cache before install
    apt: update_cache=yes
    
  - name: install PostgreSQL {{pg_version}}
    apt:
      name: postgresql-{{pg_version}}
      state: present

Save it as for example “install_postgresql_ubuntu.yml” file and define host IP in local inventory file (here named hosts_inventory) and run it like this:

ansible-playbook -i hosts_inventory install_postgresql_ubuntu.yml --ask-become-pass -e pgversion=9.5