# Update lists and upgrade packages.

- name: update apt package lists
  apt:
    update_cache: true
    cache_valid_time: 86400

- block:
    - name: upgrade packages
      apt:
        upgrade: dist
        autoremove: true
        autoclean: true
  rescue:
    - name: Looks like dpkg was interrupted, configure manually
      command:
        cmd: dpkg --configure -a
    - name: Try again to upgrade packages
      apt:
        upgrade: dist
        autoremove: true
        autoclean: true

- name: install etckeeper
  apt:
    name: etckeeper
    state: latest # noqa package-latest

- name: install extra packages from stable
  apt:
    name: "{{ extra_pkgs }}"
    state: latest # noqa package-latest
  when: extra_pkgs|length

- name: add {{ ansible_distribution_release }}-backports
  apt_repository:
    repo: >
      deb http://deb.debian.org/debian/ {{ ansible_distribution_release }}-backports
      main non-free-firmware
    state: present
    update_cache: true
  when: extra_pkgs_bpo|length

- name: install extra packages from backports
  apt:
    name: "{{ extra_pkgs_bpo }}"
    state: latest # noqa package-latest
    default_release: "{{ ansible_distribution_release }}-backports"
  when: extra_pkgs_bpo|length
