Skip to content

Commit ab21aba

Browse files
committed
devconfig: add automatic APT mirror fallback with DEB822 modernization
Debian testing (trixie) VMs can fail to provision when configured APT mirrors become unavailable or unresponsive. This is particularly common with local or regional mirrors that may have intermittent connectivity issues. This fix adds automatic mirror health checking specifically for Debian testing systems. The implementation: 1. Detects current APT format (legacy sources.list or DEB822) 2. Extracts the configured mirror hostname from either format 3. Tests connectivity to the mirror on port 80 with 10 second timeout 4. Falls back to official Debian mirrors if the test fails 5. Backs up the original sources before making changes 6. Applies the new sources in modern DEB822 format 7. Removes legacy sources.list when migrating from old format 8. Updates the APT cache after switching mirrors The check only runs on Debian testing systems where devconfig_debian_testing is set to true, avoiding any impact on stable Debian or other distributions. By modernizing to DEB822 format during fallback, we align with Debian's recommended configuration while ensuring VMs can successfully provision even when the initially configured mirror is unavailable. Generated-by: Claude AI Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
1 parent 7dbe6ca commit ab21aba

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
# Only run mirror checks for Debian testing (trixie) where mirror issues are common
3+
- name: Check for DEB822-style sources
4+
stat:
5+
path: /etc/apt/sources.list.d/debian.sources
6+
register: deb822_sources
7+
8+
- name: Extract current APT mirror hostname (DEB822 format)
9+
shell: |
10+
grep -E "^URIs:" /etc/apt/sources.list.d/debian.sources | head -1 | awk '{print $2}' | sed -E 's|https?://||' | cut -d'/' -f1
11+
register: apt_mirror_host_deb822
12+
changed_when: false
13+
ignore_errors: yes
14+
when: deb822_sources.stat.exists
15+
16+
- name: Extract current APT mirror hostname (legacy format)
17+
shell: |
18+
grep -E "^deb\s+http" /etc/apt/sources.list | head -1 | awk '{print $2}' | sed 's|http://||' | cut -d'/' -f1
19+
register: apt_mirror_host_legacy
20+
changed_when: false
21+
ignore_errors: yes
22+
when: not deb822_sources.stat.exists
23+
24+
- name: Set unified mirror hostname
25+
set_fact:
26+
apt_mirror_host:
27+
stdout: "{{ apt_mirror_host_deb822.stdout if deb822_sources.stat.exists else apt_mirror_host_legacy.stdout }}"
28+
29+
- name: Check connectivity to current APT mirror
30+
wait_for:
31+
host: "{{ apt_mirror_host.stdout }}"
32+
port: 80
33+
timeout: 10
34+
register: mirror_connectivity
35+
ignore_errors: yes
36+
when: apt_mirror_host.stdout != ""
37+
38+
- name: Display mirror check results
39+
debug:
40+
msg: |
41+
Current APT mirror: {{ apt_mirror_host.stdout | default('Not found') }}
42+
Mirror connectivity: {{ 'OK' if mirror_connectivity is not failed else 'FAILED' }}
43+
when: apt_mirror_host.stdout != ""
44+
45+
- name: Fall back to official Debian mirrors if current mirror fails
46+
block:
47+
- name: Backup current sources (DEB822 format)
48+
copy:
49+
src: /etc/apt/sources.list.d/debian.sources
50+
dest: /etc/apt/sources.list.d/debian.sources.backup
51+
remote_src: yes
52+
become: yes
53+
when: deb822_sources.stat.exists
54+
55+
- name: Backup current sources (legacy format)
56+
copy:
57+
src: /etc/apt/sources.list
58+
dest: /etc/apt/sources.list.backup
59+
remote_src: yes
60+
become: yes
61+
when: not deb822_sources.stat.exists
62+
63+
- name: Apply Debian testing fallback sources using modern DEB822 format
64+
template:
65+
src: debian-testing-fallback.sources
66+
dest: /etc/apt/sources.list.d/debian.sources
67+
owner: root
68+
group: root
69+
mode: '0644'
70+
become: yes
71+
72+
- name: Remove legacy sources.list if migrating to DEB822
73+
file:
74+
path: /etc/apt/sources.list
75+
state: absent
76+
become: yes
77+
when: not deb822_sources.stat.exists
78+
79+
- name: Update APT cache after mirror change
80+
apt:
81+
update_cache: yes
82+
cache_valid_time: 0
83+
become: yes
84+
85+
- name: Inform user about mirror fallback
86+
debug:
87+
msg: |
88+
WARNING: The configured APT mirror '{{ apt_mirror_host.stdout }}' is not accessible.
89+
Falling back to official Debian testing mirrors using modern DEB822 format:
90+
- deb.debian.org for main packages
91+
- security.debian.org for security updates
92+
93+
Your sources have been migrated to /etc/apt/sources.list.d/debian.sources
94+
This may result in slower package downloads depending on your location.
95+
Consider configuring a local mirror for better performance.
96+
97+
when:
98+
- apt_mirror_host.stdout != ""
99+
- mirror_connectivity is failed

playbooks/roles/devconfig/tasks/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
tags: hostname
3131

3232
# Distro specific
33+
34+
# Check and fix APT mirrors for Debian testing before installing dependencies
35+
- name: Check and fix APT mirrors for Debian testing
36+
include_tasks: check-apt-mirrors.yml
37+
when:
38+
- devconfig_debian_testing is defined
39+
- devconfig_debian_testing | bool
40+
3341
- name: Install dependencies
3442
ansible.builtin.include_tasks: install-deps/main.yml
3543
tags: ['vars', 'vars_simple']
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Types: deb deb-src
2+
URIs: https://deb.debian.org/debian
3+
Suites: testing testing-updates
4+
Components: main contrib non-free non-free-firmware
5+
Enabled: yes
6+
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
7+
8+
Types: deb deb-src
9+
URIs: https://security.debian.org/debian-security
10+
Suites: testing-security
11+
Components: main contrib non-free non-free-firmware
12+
Enabled: yes
13+
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

0 commit comments

Comments
 (0)