---
- name: Upgrade Client from Bookworm to Trixie
  hosts: all
  remote_user: ansible
  become: true

  tasks:
    - name: Update system to latest Bookworm
      ansible.builtin.apt:
        upgrade: full
        update_cache: false
        dpkg_options: "force-confdef,force-confold"
      environment:
        DEBIAN_FRONTEND: noninteractive

    - name: Run apt autoremove
      ansible.builtin.apt:
        autoremove: true

    - name: Remove backports for {{ ansible_distribution_release }}
      ansible.builtin.apt_repository:
        repo: "deb http://deb.debian.org/debian/ {{ ansible_distribution_release }}-backports main non-free-firmware"
        state: absent
        update_cache: false

    - name: Change Debian Release from Bookworm to Trixie in /etc/apt/sources.list
      ansible.builtin.replace:
        path: "/etc/apt/sources.list"
        regexp: "bookworm"
        replace: "trixie"

    - name: Run full package upgrade to Trixie
      ansible.builtin.apt:
        update_cache: true
        upgrade: full
        dpkg_options: "force-confdef,force-confold"
      environment:
        DEBIAN_FRONTEND: noninteractive

    - name: Reboot client
      ansible.builtin.reboot:
        msg: "Client Reboot after Upgrade to Trixie"
        reboot_timeout: 600

    - name: Wait until the client is reachable after reboot
      ansible.builtin.wait_for_connection:
        timeout: 600

    - name: Find all user home directories
      ansible.builtin.find:
        paths: /home
        file_type: directory
      register: user_homes

    - name: Remove Nextcloud from Dolphin bookmarks
      ansible.builtin.command: >
        sed -i '/<bookmark href="file:\/\/\/lmn\/media\/.*\/nextcloud">/,/<\/bookmark>/d' {{ item }}/.local/share/user-places.xbel
      loop: "{{ user_homes.files | map(attribute='path') | list }}"
      when: item is match('^/home/.+')
      ignore_errors: true

    - name: Remove PAM Bind-Mounts block
      ansible.builtin.blockinfile:
        path: /etc/security/pam_mount.conf.xml
        state: absent
        marker: "<!-- {mark} ANSIBLE MANAGED BLOCK $bind mounts for VMs$ -->"

    - name: Remove PAM Nextcloud-Mounts block
      ansible.builtin.blockinfile:
        path: /etc/security/pam_mount.conf.xml
        state: absent
        marker: "<!-- {mark} ANSIBLE MANAGED BLOCK $mount Nextcloud$ -->"

    - name: Install qemu spice support
      ansible.builtin.apt:
        name: qemu-system-modules-spice

    - name: Disable systemd-networkd.service
      ansible.builtin.systemd:
        name: systemd-networkd.service
        enabled: false


- import_playbook: lmn-client.yml
