---
  - file: path=/opt/upgrader state=directory mode=0755

  - stat: path=/opt/upgrader/playbook
    register: playbook_folder

  - stat: path=/opt/upgrader/last-upgrade
    register: last_upgrade

  - shell: echo 1 > /opt/upgrader/last-upgrade
    when: last_upgrade.stat.exists == False

  - file: path=/opt/upgrader/ state=directory mode=0755

  - shell: rm -rf /opt/upgrader/tmp

  - shell: mkdir -p /opt/upgrader/tmp

  - name: Periodically download the latest playbook
    shell: /usr/local/bin/slapcache-download --destination=/opt/upgrader/archive.tar.gz

  - name: Uncompress
    shell: tar -xzvf /opt/upgrader/archive.tar.gz
    args:
      chdir: /opt/upgrader/tmp

  - shell: cp -R /opt/upgrader/tmp /opt/upgrader/playbook
    when: playbook_folder.stat.exists == False

  - stat: path=/opt/upgrader/tmp/hosts
    register: hosts_file

  - shell: rm -rf /opt/upgrader/playbook-tmp
    when: hosts_file.stat.exists == True

  - shell: cp -R /opt/upgrader/tmp /opt/upgrader/playbook-tmp
    when: hosts_file.stat.exists == True

  - name: Save new archive MD5
    stat:
      path=/opt/upgrader/archive.tar.gz
      get_md5=yes
    register:
      archive

  - name: Update playbook md5
    shell: echo {{ archive.stat.md5 }} > /opt/upgrader/next-upgrade-signature

  - name: Stat Current Upgrade Signature
    file: path=/opt/upgrader/current-upgrade-signature state=touch mode="0666"

  - name: Get /opt/upgrader/next-upgrade-signature content
    shell: cat /opt/upgrader/next-upgrade-signature
    register:
      next_upgrade_signature

  - name: Get /opt/upgrader/current-upgrade-signature content
    shell: cat /opt/upgrader/current-upgrade-signature
    register:
      current_upgrade_signature

  - name: Do upgrade
    shell: ansible-playbook {{ upgrader_playbook }} -i hosts 2>>/opt/upgrader/latest_upgrade.log  >> /opt/upgrader/latest_upgrade.log
    when: (upgrade_after > lookup('file', '/opt/upgrader/last-upgrade')|int ) and (lookup('file', '/opt/upgrader/next-upgrade-signature') != lookup('file', '/opt/upgrader/current-upgrade-signature'))
    args:
      chdir: /opt/upgrader/playbook-tmp/

  - name: Set Cron
    cron: name="Launch Upgrader with ansible" minute="{{ period_minute }}" hour="{{ period_hour }}" job="flock -n /opt/upgrader/ansible.lock -c \"rm -rf /opt/upgrader/playbook && cp -R /opt/upgrader/playbook-tmp /opt/upgrader/playbook && cd /opt/upgrader/playbook && PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ansible-playbook upgrader-run.yml --extra-vars 'period_minute={{ period_minute }} period_hour={{ period_hour }} upgrader_playbook={{ upgrader_playbook }} upgrade_kernel={{ upgrade_kernel | default(False) == True }}' -i hosts 2>>/opt/upgrader/latest_upgrade.log >> /opt/upgrader/latest_upgrade.log\""

  - cron: name="Launch Upgrader" state=absent

  - name: Upgrade Current Upgrade Signature
    shell: cat /opt/upgrader/next-upgrade-signature > /opt/upgrader/current-upgrade-signature
    when: (upgrade_after > lookup('file', '/opt/upgrader/last-upgrade')|int ) and (lookup('file', '/opt/upgrader/next-upgrade-signature') != lookup('file', '/opt/upgrader/current-upgrade-signature'))

  - shell: echo {{ ansible_date_time.epoch }} > /opt/upgrader/last-upgrade
    when: (upgrade_after > lookup('file', '/opt/upgrader/last-upgrade')|int )

  - include: remove-legacy.yml
