---

  - name: Download hostname
    get_url: url=http://10.0.2.100/hostname dest=/etc/opt/hostname mode=666
    ignore_errors: True

  - name: Download ipv4
    get_url: url=http://10.0.2.100/ipv4 dest=/etc/opt/ipv4 mode=666
    ignore_errors: True

  - name: stat /etc/opt/hostname
    stat: path=/etc/opt/hostname
    register: hostname_file

  - name: stat /etc/opt/ipv4
    stat: path=/etc/opt/ipv4
    register: ipv4_file

  - name: stat cluster.hash
    stat: path=/etc/opt/cluster.hash
    register: cluster_hash

  - name: replace /etc/hostname
    copy: src=/etc/opt/hostname dest=/etc/hostname mode=666
    when: hostname_file.stat.exists == True

  - name: update hostname
    command: hostname -F /etc/hostname
    when: hostname_file.stat.exists == True

  - name: managing hosts file 
    lineinfile: dest=/etc/hosts regexp="^127.0.1.1" line="" state=absent
    when: hostname_file.stat.exists == True

  - name: setting hosts special line
    lineinfile: dest=/etc/hosts line="{{ lookup('file', '/etc/opt/ipv4') }}  {{ lookup('file', '/etc/opt/hostname') }}.tl.teralab-datascience.fr {{ lookup('file', '/etc/opt/hostname') }}" 
    when: hostname_file.stat.exists == True

  - name: setting cluster 
    shell: wget --no-check-certificate "{{ lookup('file', '/etc/opt/cluster.hash') }}/hosts" -O /tmp/hosts
    when: cluster_hash.stat.exists == True

  - name: stat /tmp/hosts
    stat: path=/tmp/hosts
    register: hosts_file

  - name: Format hosts
    script: format_hosts /tmp/hosts tl.teralab-datascience.fr 
    when: cluster_hash.stat.exists == True

  - name: managing resolv.conf file
    lineinfile: dest=/etc/resolv.conf line="search tl.teralab-datascience.fr"

  - name: adding entry from workspace
    lineinfile: dest=/etc/resolv.conf line="nameserver 10.200.218.1"

  - name: Delete default nameserver
    lineinfile: dest=/etc/resolv.conf line="nameserver 10.0.2.3" state=absent

  - name: Delete nameserver 8.8.8.8
    lineinfile: dest=/etc/resolv.conf line="nameserver 8.8.8.8" state=absent

  - name: Turn off dhclient if it's running
    shell: pkill dhclient
    ignore_errors: True

  - name: ping current host
    script: ping host {{ lookup('file', '/etc/opt/ipv4') }} {{ lookup('file', '/etc/opt/hostname') }}
    when: hostname_file.stat.exists == True

  - name: ping cluster hosts
    script: ping cluster /tmp/hosts
    when: hosts_file.stat.exists == True
    ignore_errors: True
