---
  - name: Add startup script
    template: src=start-script.j2 dest=/usr/local/bin/vm-bootstrap-update mode=755

  - name: get periodicity time
    shell: cat /etc/opt/periodicity || echo 10
    register: periodicity
    ignore_errors: True

  - name: Add a periodical update on the VM
    cron: name="Update vm bootstrap"
          minute="*/{{ periodicity.stdout }}"
          job="bash -lc /usr/local/bin/vm-bootstrap-update >> /var/log/vm-bootstrap.log 2>&1"
          cron_file=ansible-vm-bootstrap user="root"

  - name: Add on reboot update on the VM
    cron: name="reboot run vm bootstrap"
          special_time=reboot
          job="WAIT_TIME=10 bash -lc /usr/local/bin/vm-bootstrap-update >> /var/log/vm-bootstrap.log 2>&1"
          cron_file=ansible-vm-bootstrap user="root"

  - name: Add PATH env variable to cron
    lineinfile: dest=/etc/cron.d/ansible-vm-bootstrap
          line=PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
          insertbefore=BOF state=present

  - name: Check if /opt/slapos.playbook already exists
    file: path=/opt/slapos.playbook/ state=directory

  - name: check if playbook is there
    stat: path=/opt/slapos.playbook/playbook.tar.gz
    register: playbook_archive

  - name: Download playbook
    shell: slapcache-download --destination=playbook.tar.gz
    when: playbook_archive.stat.exists == False
    args:
      chdir: /opt/slapos.playbook

  - name: Copy slapos.playbook
    shell: tar -xzvf /opt/slapos.playbook/playbook.tar.gz
    when: playbook_archive.stat.exists == False
    args:
      chdir: /opt/slapos.playbook


