---
- name: Ensure all needed packages are installed (Ubuntu)
  when: is_debian_or_ubuntu
  action: apt pkg={{item}} state=installed update_cache=yes
  with_items:
    - default-jdk
    - sudo

- name: Ensure all needed packages are installed (CentOS)
  when: is_centos
  action: yum pkg={{item}} state=installed
  with_items:
    - java-openjdk
    - sudo

- name: download and install hadoop debian package
  when: is_debian_or_ubuntu and hadoop_tar_url is not defined
  shell: dpkg -l hadoop | grep ^ii || (tmpfile=$(mktemp tmp.XXXXXXXXX.deb); wget {{deburl}} -O $tmpfile -o /dev/null; dpkg -i $tmpfile; rc=$?; rm -f $tmpfile; exit $rc)

- name: download and install hadoop rpm package
  when: is_centos
  action: shell rpm -q hadoop || rpm -i {{rpmurl}}

- name: Download hadoop tar.gz file
  shell: "[ -f /tmp/hadoop.tgz ] || wget {{hadoop_tar_url}} -O /tmp/hadoop.tgz"
  register: hadoop_downloaded
  when: hadoop_tar_url is defined

- name: Install hadoop from tar.gz
  shell: tar --transform 's:/*hadoop[^/]*/::' -C /usr -zxf /tmp/hadoop.tgz
  when: hadoop_downloaded|success and hadoop_tar_url is defined
  register: hadoop_uncompressed

- name: Fix path to /etc/hadoop
  file: src=/usr/etc/hadoop dest=/etc/hadoop state=link
  when: hadoop_uncompressed|success and hadoop_tar_url is defined
