---
- block:

  - name: Remove older gdal related packages
    # Uninstall older gdal packages which come from ubuntu's default apt
    # sources
    apt:
      name:
        - "libgdal1h"
        - "python-gdal"
      state: "absent"
      purge: "yes"

  - name: Install dependent packages from apt for python-gdal
    apt:
      name:
        - "libgdal20"
        - "libgdal-dev"
        - "gdal-bin"
      install_recommends: no
      cache_valid_time: "{{ apt_cache_valid_time }}"

  - name: Install gdal using pip
    # Not using pip module as we need to provide few environment variables
    # for the gdal installation to work, which is not possible using pip module
    shell: "C_INCLUDE_PATH=/usr/include/gdal CPLUS_INCLUDE_PATH=/usr/include/gdal pip2.7 install GDAL==`gdal-config --version`"
    register: gdal_installed
    changed_when: "'Successfully installed' in gdal_installed.stdout"

  become: yes
