---
- name: Pull bootstrap images into containerd
  shell: |
    if /usr/local/bin/k3s crictl inspecti "{{ item }}" >/dev/null 2>&1; then
      echo "already present"
      exit 0
    fi

    echo "mtu=$(cat /sys/class/net/{{ ansible_default_ipv4.interface | default('eth0') }}/mtu 2>/dev/null || true)"
    ip route get 1.1.1.1 || true

    for attempt in 1 2 3 4 5 6 7 8 9 10; do
      echo "pull attempt ${attempt}: {{ item }}"
      if timeout 240s /usr/local/bin/k3s crictl pull "{{ item }}"; then
        echo "pulled image"
        exit 0
      fi

      sleep 15
    done

    exit 1
  args:
    executable: /bin/bash
  register: bootstrap_image_pull
  loop: "{{ bootstrap_prepull_images }}"
  changed_when: "'pulled image' in bootstrap_image_pull.stdout"
