---
- name: Fix Podman installation on remaining nodes
  hosts: semaphore,master,ash3c,hcs
  become: yes
  serial: 1  # 逐个处理，避免同时影响多个节点
  
  tasks:
    - name: Current node status
      debug:
        msg: "🔧 修复节点: {{ inventory_hostname }}"
    
    - name: Check if Podman is already installed
      shell: podman --version 2>/dev/null || echo "NOT_INSTALLED"
      register: podman_check
      
    - name: Install Podman if not present (semaphore special handling)
      apt:
        name:
          - podman
          - buildah
          - skopeo
        state: present
        update_cache: yes
        force_apt_get: yes
      when: inventory_hostname == 'semaphore' and 'NOT_INSTALLED' in podman_check.stdout
      ignore_errors: yes
      
    - name: Install Podman on other nodes
      apt:
        name:
          - podman
          - buildah  
          - skopeo
        state: present
      when: inventory_hostname != 'semaphore'
      ignore_errors: yes
      
    - name: Install Python dependencies for podman-compose
      apt:
        name:
          - python3-pip
          - python3-setuptools
          - python3-yaml
          - python3-dotenv
        state: present
      ignore_errors: yes
      
    - name: Install podman-compose via pip
      pip:
        name: 
          - podman-compose
        state: present
        executable: pip3
      ignore_errors: yes
      
    - name: Alternative podman-compose installation via apt
      apt:
        name: podman-compose
        state: present
      ignore_errors: yes
      
    - name: Verify installations
      shell: |
        echo "Podman: $(podman --version 2>/dev/null || echo 'FAILED')"
        echo "Podman Compose: $(podman-compose --version 2>/dev/null || echo 'FAILED')"
      register: verify_result
      
    - name: Display verification results
      debug:
        msg: |
          ✅ 节点 {{ inventory_hostname }} 验证结果:
          {{ verify_result.stdout }}
          
    - name: Enable Podman socket
      systemd:
        name: podman.socket
        enabled: yes
        state: started
      ignore_errors: yes