From 585fa54336e874d6dc0acc670cbb4048c1ef5e36 Mon Sep 17 00:00:00 2001 From: alex-local Date: Wed, 10 Sep 2025 12:34:46 +0200 Subject: [PATCH] Add comprehensive APT upgrade playbook with reboot support --- apt-upgrade.yml | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 apt-upgrade.yml diff --git a/apt-upgrade.yml b/apt-upgrade.yml new file mode 100644 index 0000000..3313541 --- /dev/null +++ b/apt-upgrade.yml @@ -0,0 +1,56 @@ +--- +- name: APT Update and Upgrade Playbook + hosts: all + become: yes + gather_facts: yes + + tasks: + - name: Update apt package cache + ansible.builtin.apt: + update_cache: yes + cache_valid_time: 3600 + register: apt_update_result + when: ansible_os_family == "Debian" + + - name: Upgrade all packages to latest version + ansible.builtin.apt: + upgrade: dist + autoremove: yes + autoclean: yes + register: apt_upgrade_result + when: ansible_os_family == "Debian" and apt_update_result is succeeded + + - name: Check if reboot is required + ansible.builtin.stat: + path: /var/run/reboot-required + register: reboot_required_file + when: ansible_os_family == "Debian" + + - name: Display upgrade summary + ansible.builtin.debug: + msg: | + APT operations completed on {{ inventory_hostname }}: + - Cache updated: {{ apt_update_result.changed | default(false) }} + - Packages upgraded: {{ apt_upgrade_result.changed | default(false) }} + - Reboot required: {{ reboot_required_file.stat.exists | default(false) }} + when: ansible_os_family == "Debian" + + - name: Reboot system if required + ansible.builtin.reboot: + msg: "Reboot initiated by Ansible for package updates" + connect_timeout: 5 + reboot_timeout: 300 + pre_reboot_delay: 0 + post_reboot_delay: 30 + test_command: uptime + when: ansible_os_family == "Debian" and reboot_required_file.stat.exists | default(false) + + - name: Skip non-Debian systems + ansible.builtin.debug: + msg: "Skipping {{ inventory_hostname }} - not a Debian/Ubuntu system ({{ ansible_os_family }})" + when: ansible_os_family != "Debian" + + - name: Display failure message + ansible.builtin.debug: + msg: "APT operations failed on {{ inventory_hostname }}" + when: ansible_os_family == "Debian" and (apt_update_result is failed or apt_upgrade_result is failed)