---

  - name: Install Debian stable-proposed-updates repository
    apt_repository: repo='deb http://deb.debian.org/debian/ oldstable-proposed-updates main' state=present update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "9"

  - name: Install kernel 4.9 on debian 9.0
    apt: name=linux-image-4.9.0-11-amd64  state=latest default_release=oldstable-proposed-updates update_cache=no
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "9"
    notify: [ 'Mark to reboot' ]

  - name: Install Debian jessie-backports repository
    apt_repository: repo='deb http://ftp.debian.org/debian jessie-backports main' state=present update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"

  - name: Install kernel 4.9 on debian 8.0
    apt: name=linux-image-4.9.0-0.bpo.9-amd64  state=latest default_release=jessie-backports update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "8"
    notify: [ 'Mark to reboot' ]

  - name: Install Debian jessie repository on debian 7
    lineinfile:
      dest: '/etc/apt/sources.list'
      line: "{{item.line}}"
      state: present
    with_items:
      - {line: "deb http://deb.debian.org/debian jessie main"}
      - {line: "deb-src http://deb.debian.org/debian jessie main"}
      - {line: "deb http://security.debian.org/ jessie/updates main contrib non-free"}
      - {line: "deb-src http://security.debian.org/ jessie/updates main contrib non-free"}
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "7"

  - name: Dist upgrade debian 7 to debian 8
    apt: upgrade=dist update_cache=yes
    when: ansible_distribution == "Debian" and ansible_distribution_major_version == "7"
    notify: [ 'Mark to reboot' ]

  - name: create trigger reboot script
    copy: src=slapos-trigger-reboot.sh dest=/usr/local/bin/slapos-trigger-reboot.sh mode=700

  - name: Stat AUTOREBOOT
    stat: path=/opt/upgrader/AUTOREBOOT
    register: autoreboot

  - name: Setup auto-reboot cron
    cron: name="Launch Reboot when REBOOT is Required" minute="*/30" job='/usr/local/bin/slapos-trigger-reboot.sh'
    when: autoreboot.stat.exists == True
