---
- name: Update all servers (FreeBSD, Debian)
  hosts: all
  become: yes
  gather_facts: yes

  tasks:

    ##################################################
    # Debian / Ubuntu
    ##################################################
    - name: Update package cache (Debian/Ubuntu)
      apt:
        update_cache: yes
      when: ansible_facts['os_family'] == "Debian"

    - name: Upgrade all packages (Debian/Ubuntu)
      apt:
        upgrade: dist
      register: debian_update
      when: ansible_facts['os_family'] == "Debian"

    - name: Reboot Debian only if packages changed
      reboot:
      when:
        - ansible_facts['os_family'] == "Debian"
        - debian_update is defined
        - debian_update.changed

    ##################################################
    # Arch Linux
    ##################################################
    - name: Upgrade all packages (Arch Linux)
      community.general.pacman:
        update_cache: yes
        upgrade: yes
      register: arch_update
      when: ansible_facts['os_family'] == "Archlinux"

    - name: Reboot Arch only if packages changed
      reboot:
      when:
        - ansible_facts['os_family'] == "Archlinux"
        - arch_update is defined
        - arch_update.changed
