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

    pg_version: "{{ pgversion }}"


  - 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
      repo: "deb {{ubuntu_release.stdout}}-pgdg main"
      state: present

  - name: import repository keys     
      state: present

  - name: update cache before install
    apt: update_cache=yes
  - name: install PostgreSQL {{pg_version}}
      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