57 lines
2.0 KiB
YAML
57 lines
2.0 KiB
YAML
---
|
|
- 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)
|