Files
ansible/update.yml

63 lines
2.4 KiB
YAML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
- name: Безопасное обновление системы Linux
hosts: all
become: true
gather_facts: true
tasks:
- name: Обновить кэш и пакеты (Debian/Ubuntu)
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600
upgrade: full
autoremove: yes
when: ansible_os_family == "Debian"
register: apt_update
- name: Обновить кэш и пакеты (RHEL 8+/Fedora/Alma/Rocky)
ansible.builtin.dnf:
update_cache: yes
name: '*'
state: latest
when: ansible_os_family == "RedHat" and ansible_pkg_mgr == "dnf"
register: dnf_update
- name: Обновить кэш и пакеты (Legacy RHEL/CentOS 7)
ansible.builtin.yum:
update_cache: yes
name: '*'
state: latest
when: ansible_os_family == "RedHat" and ansible_pkg_mgr == "yum"
register: yum_update
- name: Вывести отчёт об обновлении
ansible.builtin.debug:
msg: >-
Система: {{ ansible_distribution }} {{ ansible_distribution_version }}
Статус: {{ "Применены обновления" if (apt_update.changed | default(false)) or (dnf_update.changed | default(false)) or (yum_update.changed | default(false)) else "Всё актуально" }}
changed_when: false
# Опционально: безопасная перезагрузка при обновлении ядра
- name: Проверить необходимость перезагрузки (Debian)
ansible.builtin.stat:
path: /var/run/reboot-required
register: reboot_deb
when: ansible_os_family == "Debian"
changed_when: false
- name: Проверить необходимость перезагрузки (RHEL)
ansible.builtin.command: dnf needs-restarting -r
register: reboot_rhel
failed_when: reboot_rhel.rc not in [0, 1]
changed_when: reboot_rhel.rc == 1
when: ansible_os_family == "RedHat" and ansible_pkg_mgr == "dnf"
- name: Перезагрузить сервер при необходимости
ansible.builtin.reboot:
msg: "Обновление ядра/пакетов требует перезагрузки."
pre_reboot_delay: 15
test_command: whoami
reboot_timeout: 600
when: >
(reboot_deb.stat.exists | default(false)) or
(reboot_rhel.rc | default(0) == 1)