Update update.yml
This commit is contained in:
199
update.yml
199
update.yml
@@ -1,165 +1,82 @@
|
|||||||
|
---
|
||||||
- name: Update system (APT + Flatpak)
|
- name: Update system (APT + Flatpak)
|
||||||
hosts: all
|
hosts: all
|
||||||
gather_facts: false
|
become: yes
|
||||||
strategy: free
|
gather_facts: yes
|
||||||
serial: 2
|
serial: 5
|
||||||
|
|
||||||
become: true
|
tasks:
|
||||||
become_user: root
|
|
||||||
become_method: sudo
|
|
||||||
|
|
||||||
vars:
|
|
||||||
ssh_precheck_timeout: 8
|
|
||||||
apt_async: 1800
|
|
||||||
apt_poll: 10
|
|
||||||
apt_retries: 3
|
|
||||||
apt_retry_delay: 5
|
|
||||||
flatpak_timeout: 300
|
|
||||||
flatpak_async: 600
|
|
||||||
flatpak_poll: 5
|
|
||||||
|
|
||||||
pre_tasks:
|
|
||||||
- name: Ensure SSH is reachable (skip host if not)
|
- name: Ensure SSH is reachable (skip host if not)
|
||||||
wait_for:
|
|
||||||
host: "{{ ansible_host | default(inventory_hostname) }}"
|
|
||||||
port: "{{ ansible_port | default(22) }}"
|
|
||||||
timeout: "{{ ssh_precheck_timeout }}"
|
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
register: ssh_ok
|
wait_for:
|
||||||
ignore_errors: true
|
host: "{{ inventory_hostname }}"
|
||||||
|
port: 22
|
||||||
|
timeout: 5
|
||||||
|
register: ssh_check
|
||||||
|
ignore_errors: yes
|
||||||
|
|
||||||
- meta: end_host
|
- meta: end_host
|
||||||
when: ssh_ok is failed
|
when: ssh_check is failed
|
||||||
|
|
||||||
- name: Ping with retries (handle intermittent flaps)
|
- name: Ping with retries (handle intermittent flaps)
|
||||||
ping:
|
ping:
|
||||||
register: ping_r
|
register: ping_result
|
||||||
retries: 3
|
retries: 5
|
||||||
delay: 3
|
delay: 5
|
||||||
until: ping_r is succeeded
|
until: ping_result is success
|
||||||
ignore_errors: true
|
|
||||||
|
|
||||||
- meta: end_host
|
- name: Wait for apt lock to be released
|
||||||
when: ping_r is failed
|
shell: |
|
||||||
|
while fuser /var/lib/dpkg/lock-frontend >/dev/null 2>&1; do
|
||||||
tasks:
|
echo "Waiting for apt lock..."
|
||||||
- name: Update APT cache (bounded + retried)
|
sleep 5
|
||||||
environment: { DEBIAN_FRONTEND: noninteractive }
|
done
|
||||||
apt:
|
|
||||||
update_cache: yes
|
|
||||||
cache_valid_time: 3600
|
|
||||||
async: "{{ apt_async }}"
|
|
||||||
poll: "{{ apt_poll }}"
|
|
||||||
register: apt_update
|
|
||||||
retries: "{{ apt_retries }}"
|
|
||||||
delay: "{{ apt_retry_delay }}"
|
|
||||||
until: apt_update is succeeded
|
|
||||||
|
|
||||||
- name: If APT cache update failed, try to fix dpkg and retry once
|
|
||||||
block:
|
|
||||||
- name: Fix partially configured packages
|
|
||||||
command: dpkg --configure -a
|
|
||||||
changed_when: false
|
changed_when: false
|
||||||
- name: Retry APT cache update after dpkg fix
|
|
||||||
environment: { DEBIAN_FRONTEND: noninteractive }
|
- name: Update apt cache
|
||||||
apt:
|
apt:
|
||||||
update_cache: yes
|
update_cache: yes
|
||||||
async: 600
|
|
||||||
poll: 5
|
|
||||||
when: apt_update is failed
|
|
||||||
|
|
||||||
- name: Upgrade all APT packages (bounded + retried)
|
- name: Perform full upgrade
|
||||||
environment: { DEBIAN_FRONTEND: noninteractive }
|
|
||||||
apt:
|
apt:
|
||||||
upgrade: dist
|
upgrade: full
|
||||||
async: "{{ apt_async }}"
|
autoremove: yes
|
||||||
poll: "{{ apt_poll }}"
|
autoclean: yes
|
||||||
register: apt_upgrade
|
register: apt_upgrade
|
||||||
retries: "{{ apt_retries }}"
|
retries: 3
|
||||||
delay: "{{ apt_retry_delay }}"
|
delay: 10
|
||||||
until: apt_upgrade is succeeded
|
until: apt_upgrade is succeeded
|
||||||
|
|
||||||
- name: If APT upgrade failed, try to fix dpkg and retry once
|
- name: Fix broken packages
|
||||||
block:
|
command: apt-get -f install -y
|
||||||
- name: Fix partially configured packages
|
register: fix_result
|
||||||
command: dpkg --configure -a
|
failed_when: false
|
||||||
|
changed_when: "'Setting up' in fix_result.stdout"
|
||||||
|
|
||||||
|
- name: Check if Flatpak is installed
|
||||||
|
command: which flatpak
|
||||||
|
register: flatpak_check
|
||||||
|
failed_when: false
|
||||||
changed_when: false
|
changed_when: false
|
||||||
- name: Retry APT upgrade after dpkg fix
|
|
||||||
environment: { DEBIAN_FRONTEND: noninteractive }
|
|
||||||
apt:
|
|
||||||
upgrade: dist
|
|
||||||
async: 1200
|
|
||||||
poll: 5
|
|
||||||
when: apt_upgrade is failed
|
|
||||||
|
|
||||||
- name: Check if flatpak binary exists
|
- name: Update Flatpak packages
|
||||||
become: false
|
command: flatpak update -y
|
||||||
|
when: flatpak_check.rc == 0
|
||||||
|
register: flatpak_update
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Remove unused Flatpak packages
|
||||||
|
command: flatpak uninstall --unused -y
|
||||||
|
when: flatpak_check.rc == 0
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
|
- name: Check if reboot is required
|
||||||
stat:
|
stat:
|
||||||
path: /usr/bin/flatpak
|
path: /var/run/reboot-required
|
||||||
register: flatpak_bin
|
register: reboot_required
|
||||||
|
|
||||||
- name: Update system Flatpaks (bounded; treat timeout as non-fatal)
|
- name: Notify if reboot required
|
||||||
command: bash -lc "timeout {{ flatpak_timeout }} flatpak update -y --noninteractive"
|
debug:
|
||||||
register: flatpak_sys
|
msg: "Reboot required on {{ inventory_hostname }}"
|
||||||
async: "{{ flatpak_async }}"
|
when: reboot_required.stat.exists
|
||||||
poll: "{{ flatpak_poll }}"
|
|
||||||
failed_when: flatpak_sys.rc is defined and flatpak_sys.rc not in [0, 124]
|
|
||||||
when: flatpak_bin.stat.exists
|
|
||||||
|
|
||||||
# ---- User-agnostic Flatpak updates (all non-system users) ----
|
|
||||||
|
|
||||||
- name: Get passwd database
|
|
||||||
getent:
|
|
||||||
database: passwd
|
|
||||||
register: ge
|
|
||||||
|
|
||||||
- name: Build list of regular users (uid >= 1000, real shells)
|
|
||||||
set_fact:
|
|
||||||
regular_users: >-
|
|
||||||
{{
|
|
||||||
ge.ansible_facts.getent_passwd
|
|
||||||
| dict2items
|
|
||||||
| map(attribute='value')
|
|
||||||
| selectattr('uid', 'defined')
|
|
||||||
| selectattr('uid', '>=', 1000)
|
|
||||||
| rejectattr('shell', 'in', ['/usr/sbin/nologin','/sbin/nologin','/bin/false'])
|
|
||||||
| list
|
|
||||||
}}
|
|
||||||
when: ge is succeeded
|
|
||||||
|
|
||||||
- name: Stat per-user runtime dir if flatpak is present
|
|
||||||
stat:
|
|
||||||
path: "/run/user/{{ item.uid }}"
|
|
||||||
loop: "{{ regular_users | default([]) }}"
|
|
||||||
loop_control:
|
|
||||||
label: "{{ item.name }}"
|
|
||||||
register: user_runtime_stats
|
|
||||||
when: flatpak_bin.stat.exists
|
|
||||||
|
|
||||||
- name: Merge runtime stats keyed by username
|
|
||||||
set_fact:
|
|
||||||
user_runtime_map: >-
|
|
||||||
{{
|
|
||||||
user_runtime_stats.results
|
|
||||||
| items2dict(key_name='item.name', value_name='stat')
|
|
||||||
}}
|
|
||||||
when: flatpak_bin.stat.exists
|
|
||||||
|
|
||||||
- name: Update user Flatpaks (use XDG_RUNTIME_DIR when available)
|
|
||||||
become_user: "{{ item.name }}"
|
|
||||||
environment: >-
|
|
||||||
{{
|
|
||||||
user_runtime_map[item.name].exists
|
|
||||||
| default(false)
|
|
||||||
| ternary({'XDG_RUNTIME_DIR': '/run/user/' ~ item.uid|string}, {})
|
|
||||||
}}
|
|
||||||
command: bash -lc "timeout {{ flatpak_timeout }} flatpak --user update -y --noninteractive"
|
|
||||||
register: flatpak_user_res
|
|
||||||
async: "{{ flatpak_async }}"
|
|
||||||
poll: "{{ flatpak_poll }}"
|
|
||||||
failed_when: flatpak_user_res.rc is defined and flatpak_user_res.rc not in [0, 124]
|
|
||||||
changed_when: "'Installing' in (flatpak_user_res.stdout | default('')) or 'Installing' in (flatpak_user_res.stderr | default('')) or 'Updating' in (flatpak_user_res.stdout | default('')) or 'Updating' in (flatpak_user_res.stderr | default(''))"
|
|
||||||
loop: "{{ regular_users | default([]) }}"
|
|
||||||
loop_control:
|
|
||||||
label: "{{ item.name }}"
|
|
||||||
when: flatpak_bin.stat.exists
|
|
||||||
Reference in New Issue
Block a user