Fix linux_reboot: use /proc/1/environ container=lxc for reliable LXC detection across all distros
This commit is contained in:
@@ -12,18 +12,17 @@
|
||||
changed_when: false
|
||||
|
||||
- name: Detect if running inside an LXC container
|
||||
# Works on both Debian and Alpine — checks /proc/1/environ for container=lxc
|
||||
# This is set by the Proxmox LXC runtime and is reliable across distros
|
||||
ansible.builtin.shell: |
|
||||
grep -q 'lxc' /proc/1/environ 2>/dev/null \
|
||||
|| systemd-detect-virt --quiet --container 2>/dev/null \
|
||||
|| [ -f /run/.containerenv ] \
|
||||
&& echo "lxc" || echo "not-lxc"
|
||||
cat /proc/1/environ | tr '\0' '\n' | grep -q '^container=lxc' && echo "lxc" || echo "none"
|
||||
register: virt_detect
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Set is_lxc fact
|
||||
ansible.builtin.set_fact:
|
||||
is_lxc: "{{ 'lxc' in virt_detect.stdout }}"
|
||||
is_lxc: "{{ virt_detect.stdout | trim == 'lxc' }}"
|
||||
|
||||
# ── Debian/Ubuntu ──────────────────────────────────────────────────────────
|
||||
- name: Get installed kernel version (Debian/Ubuntu)
|
||||
@@ -36,8 +35,6 @@
|
||||
when: ansible_os_family == "Debian"
|
||||
|
||||
- name: Normalize installed kernel version (Debian/Ubuntu)
|
||||
# dpkg reports e.g. "6.12.74-2", uname -r reports "6.12.74+deb13+1-amd64"
|
||||
# Extract just the base X.Y.Z for comparison
|
||||
ansible.builtin.set_fact:
|
||||
installed_kernel_version: "{{ installed_kernel_deb.stdout | trim }}"
|
||||
installed_kernel_base: "{{ installed_kernel_deb.stdout | trim | regex_replace('^(\\d+\\.\\d+\\.\\d+).*', '\\1') }}"
|
||||
@@ -76,8 +73,8 @@
|
||||
running_kernel_base: "{{ running_kernel.stdout | trim | regex_replace('^(\\d+\\.\\d+\\.\\d+).*', '\\1') }}"
|
||||
when: ansible_os_family == "RedHat"
|
||||
|
||||
# ── Fallbacks ──────────────────────────────────────────────────────────────
|
||||
- name: Set fallback for unknown/LXC hosts
|
||||
# ── Fallback ───────────────────────────────────────────────────────────────
|
||||
- name: Set fallback for unknown hosts
|
||||
ansible.builtin.set_fact:
|
||||
installed_kernel_version: "unknown"
|
||||
installed_kernel_base: "unknown"
|
||||
@@ -101,6 +98,7 @@
|
||||
{{ inventory_hostname }}:
|
||||
running={{ running_kernel.stdout | trim }} (base={{ running_kernel_base }}),
|
||||
installed={{ installed_kernel_version }} (base={{ installed_kernel_base }}),
|
||||
virt={{ virt_detect.stdout | trim }},
|
||||
is_lxc={{ is_lxc }},
|
||||
reboot_needed={{ reboot_needed }},
|
||||
force_reboot={{ force_reboot }}
|
||||
|
||||
Reference in New Issue
Block a user