- name: update repository index
  apt:
    force_apt_get: yes
    update_cache: yes
  changed_when: no


- name: ensure apt-show-versions is installed
  apt:
    force_apt_get: yes
    name: apt-show-versions
    state: latest


- name: get upgradeable packages
  shell:
    cmd: apt-show-versions --upgradeable
  register: upgradeable
  changed_when: no


- block:
  - name: pause and confirm updates
    pause:
      prompt: "{{ upgradeable.stdout }}"


  - name: upgrade all packages
    apt:
      force_apt_get: yes
      install_recommends: no
      upgrade: dist

  when: "(upgradeable.stdout_lines is defined) and (upgradeable.stdout_lines | length > 0)"


- name: clean repository cache
  apt:
    force_apt_get: yes
    autoclean: yes
    autoremove: yes
