---
# Update and upgrade Ansible on Linux hosts
# Excludes Synology devices and handles Home Assistant carefully
# Created: February 8, 2026

- name: Update package cache and upgrade Ansible on Linux hosts
  hosts: debian_clients:!synology
  gather_facts: yes
  become: yes
  vars:
    ansible_become_pass: "{{ ansible_ssh_pass | default(omit) }}"

  tasks:
    - name: Display target host information
      debug:
        msg: "Updating Ansible on {{ inventory_hostname }} ({{ ansible_host }})"

    - name: Check if host is Home Assistant
      set_fact:
        is_homeassistant: "{{ inventory_hostname == 'homeassistant' }}"

    - name: Skip Home Assistant with warning
      debug:
        msg: "Skipping {{ inventory_hostname }} - Home Assistant uses its own package management"
      when: is_homeassistant

    - name: Update apt package cache
      apt:
        update_cache: yes
        cache_valid_time: 3600
      when: not is_homeassistant
      register: apt_update_result

    - name: Display apt update results
      debug:
        msg: "APT cache updated on {{ inventory_hostname }}"
      when: not is_homeassistant and apt_update_result is succeeded

    - name: Check current Ansible version
      command: ansible --version
      register: current_ansible_version
      changed_when: false
      failed_when: false
      when: not is_homeassistant

    - name: Display current Ansible version
      debug:
        msg: "Current Ansible version on {{ inventory_hostname }}: {{ current_ansible_version.stdout_lines[0] if current_ansible_version.stdout_lines else 'Not installed' }}"
      when: not is_homeassistant and current_ansible_version is defined

    - name: Upgrade Ansible package
      apt:
        name: ansible
        state: latest
        only_upgrade: yes
      when: not is_homeassistant
      register: ansible_upgrade_result

    - name: Display Ansible upgrade results
      debug:
        msg: |
          Ansible upgrade on {{ inventory_hostname }}:
          {% if ansible_upgrade_result.changed %}
          ✅ Ansible was upgraded successfully
          {% else %}
          ℹ️  Ansible was already at the latest version
          {% endif %}
      when: not is_homeassistant

    - name: Check new Ansible version
      command: ansible --version
      register: new_ansible_version
      changed_when: false
      when: not is_homeassistant and ansible_upgrade_result is succeeded

    - name: Display new Ansible version
      debug:
        msg: "New Ansible version on {{ inventory_hostname }}: {{ new_ansible_version.stdout_lines[0] }}"
      when: not is_homeassistant and new_ansible_version is defined

    - name: Summary of changes
      debug:
        msg: |
          Summary for {{ inventory_hostname }}:
          {% if is_homeassistant %}
          - Skipped (Home Assistant uses its own package management)
          {% else %}
          - APT cache: {{ 'Updated' if apt_update_result.changed else 'Already current' }}
          - Ansible: {{ 'Upgraded' if ansible_upgrade_result.changed else 'Already latest version' }}
          {% endif %}

  handlers:
    - name: Clean apt cache
      apt:
        autoclean: yes
      when: not is_homeassistant
