---
- name: Ensure directory {{item}} exists
  action: file path={{item}} state=directory owner=hdfs
  with_items:
    - "{{hd_confdir}}"
    - "{{hd_tmpdir}}"
    - "{{hd_namedir}}"
    - "{{hd_datadir}}"

- name: Ensure directory {{item}} exists
  action: file path={{item}} state=directory owner=mapred
  with_items:
    - "{{mapred_localdir}}"
  
- name: configure masters file
  action: template src=hadoop/templates/masters.j2 dest={{hd_confdir}}/masters owner=root mode=0644

- name: configure slaves file
  action: template src=hadoop/templates/slaves.j2 dest={{hd_confdir}}/slaves owner=root mode=0644
  when: inventory_hostname in groups.hadoop_jobtracker or inventory_hostname in groups.hadoop_namenode

- name: configure core-site.xml file
  action: template src=hadoop/templates/core-site.xml.j2 dest={{hd_confdir}}/core-site.xml owner=root mode=0644

- name: configure hdfs-site.xml file
  action: template src=hadoop/templates/hdfs-site.xml.j2 dest={{hd_confdir}}/hdfs-site.xml owner=root mode=0644

- name: configure mapred-site.xml file
  action: template src=hadoop/templates/mapred-site.xml.j2 dest={{hd_confdir}}/mapred-site.xml owner=root mode=0644

- name: set JAVA_HOME environment variable
  action: lineinfile dest={{hd_confdir}}/hadoop-env.sh regexp='export JAVA_HOME.*' line='export JAVA_HOME=/usr/lib/jvm/default-java'
  when: is_debian_or_ubuntu

- name: set JAVA_HOME environment variable
  action: lineinfile dest={{hd_confdir}}/hadoop-env.sh regexp='export JAVA_HOME.*' line='export JAVA_HOME=/usr/lib/jvm/jre'
  when: is_centos

- name: Format name directory
  action: shell sudo -u hdfs hadoop namenode -format -force -nonInteractive creates={{hd_namedir}}/image/fsimage
  when: inventory_hostname in groups.hadoop_namenode

- name: Ensure startup scripts are executables
  action: file path=/usr/sbin/{{item}} mode=755 state=file
  with_items:
    - slaves.sh
    - start-all.sh
    - start-balancer.sh
    - start-dfs.sh
    - start-jobhistoryserver.sh
    - start-mapred.sh
    - stop-all.sh
    - stop-balancer.sh
    - stop-dfs.sh
    - stop-jobhistoryserver.sh
    - stop-mapred.sh
