---

- hosts: all

  vars:
      db_name: django_app
      db_user: django
      db_password: "{{ lookup('env', 'DB_PASSWORD') }}" 
  tasks: 
    - name: Installing Packages
      sudo: yes
      apt: pkg={{ item  }} state=installed update_cache=yes
      with_items:
          - nginx
          - git
          - python-pip
          - libpq-dev
          - python-dev
          - postgresql
          - postgresql-contrib
          - python-virtualenv
          - python-psycopg2

    - name: Create Database
      sudo: yes
      sudo_user: postgres
      postgresql_db: name={{ db_name }}

    - name: Create User
      sudo: yes
      sudo_user: postgres
      postgresql_user: name={{ db_user }} password={{ db_password }} state=present role_attr_flags=NOSUPERUSER,CREATEDB

    - name: Provide user with DB permissions
      sudo: yes
      sudo_user: postgres
      postgresql_user: user={{ db_user }} db={{ db_name }} priv=ALL
