- name: Initialize Python interpreter for all nodes
  hosts: all
  gather_facts: false
  tasks:
    - name: Detect available Python interpreter on target node
      ansible.builtin.command: >
        bash -c 'command -v python3 || command -v python || command -v python2'
      register: python_interpreter_path
      changed_when: false
      failed_when: python_interpreter_path.rc != 0

    - name: Display current node's Python interpreter path
      ansible.builtin.debug:
        msg: "Node {{ inventory_hostname }} has selected Python interpreter: {{ python_interpreter_path.stdout }}"

    - name: Set ansible_python_interpreter variable for current node
      ansible.builtin.set_fact:
        ansible_python_interpreter: "{{ python_interpreter_path.stdout }}"
      delegate_to: localhost
      run_once: false