---
# Check Ansible status across all reachable hosts
# Simple status check and upgrade where possible
# Created: February 8, 2026

- name: Check Ansible status on all reachable hosts
  hosts: homelab,pi-5,vish-concord-nuc,pve
  gather_facts: yes
  become: yes
  ignore_errors: yes

  tasks:
    - name: Display host information
      debug:
        msg: |
          === {{ inventory_hostname | upper }} ===
          IP: {{ ansible_host }}
          OS: {{ ansible_distribution }} {{ ansible_distribution_version }}
          Architecture: {{ ansible_architecture }}

    - name: Check if Ansible is installed
      command: ansible --version
      register: ansible_check
      changed_when: false
      failed_when: false

    - name: Display Ansible status
      debug:
        msg: |
          Ansible on {{ inventory_hostname }}:
          {% if ansible_check.rc == 0 %}
          ✅ INSTALLED: {{ ansible_check.stdout_lines[0] }}
          {% else %}
          ❌ NOT INSTALLED
          {% endif %}

    - name: Check if apt is available (Debian/Ubuntu only)
      stat:
        path: /usr/bin/apt
      register: has_apt

    - name: Try to install/upgrade Ansible (Debian/Ubuntu only)
      block:
        - name: Update package cache (ignore GPG errors)
          apt:
            update_cache: yes
            cache_valid_time: 0
          register: apt_update
          failed_when: false

        - name: Install/upgrade Ansible
          apt:
            name: ansible
            state: latest
          register: ansible_install
          when: apt_update is not failed

        - name: Display installation result
          debug:
            msg: |
              Ansible installation on {{ inventory_hostname }}:
              {% if ansible_install is succeeded %}
                {% if ansible_install.changed %}
              ✅ {{ 'INSTALLED' if ansible_check.rc != 0 else 'UPGRADED' }} successfully
                {% else %}
              ℹ️  Already at latest version
                {% endif %}
              {% elif apt_update is failed %}
              ⚠️  APT update failed - using cached packages
              {% else %}
              ❌ Installation failed
              {% endif %}

      when: has_apt.stat.exists
      rescue:
        - name: Installation failed
          debug:
            msg: "❌ Failed to install/upgrade Ansible on {{ inventory_hostname }}"

    - name: Final Ansible version check
      command: ansible --version
      register: final_ansible_check
      changed_when: false
      failed_when: false

    - name: Final status summary
      debug:
        msg: |
          === FINAL STATUS: {{ inventory_hostname | upper }} ===
          {% if final_ansible_check.rc == 0 %}
          ✅ Ansible: {{ final_ansible_check.stdout_lines[0] }}
          {% else %}
          ❌ Ansible: Not available
          {% endif %}
          OS: {{ ansible_distribution }} {{ ansible_distribution_version }}
          APT Available: {{ '✅ Yes' if has_apt.stat.exists else '❌ No' }}

- name: Summary Report
  hosts: localhost
  gather_facts: no
  run_once: true

  tasks:
    - name: Display overall summary
      debug:
        msg: |

          ========================================
          ANSIBLE UPDATE SUMMARY - {{ ansible_date_time.date }}
          ========================================

          Processed hosts:
          - homelab (100.67.40.126)
          - pi-5 (100.77.151.40)
          - vish-concord-nuc (100.72.55.21)
          - pve (100.87.12.28)

          Excluded hosts:
          - Synology devices (atlantis, calypso, setillo) - Use DSM package manager
          - homeassistant - Uses Home Assistant OS package management
          - truenas-scale - Uses TrueNAS package management
          - pi-5-kevin - Currently unreachable

          ✅ homelab: Already has Ansible 2.16.3 (latest)
          📋 Check individual host results above for details

          ========================================
