From 84a00267443430117fdf09f3deca5a129ffe3715 Mon Sep 17 00:00:00 2001 From: administrator Date: Thu, 20 Aug 2026 00:51:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20playbooks/update.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playbooks/update.yml | 72 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/playbooks/update.yml b/playbooks/update.yml index c644cea..eeeaab3 100644 --- a/playbooks/update.yml +++ b/playbooks/update.yml @@ -2,25 +2,75 @@ - name: System update for all managed hosts hosts: all become: yes - gather_facts: yes # для получения информации о дистрибутиве + gather_facts: yes tasks: - - name: Update apt cache (Debian/Ubuntu) + # 1. Показываем текущую версию ОС и ядра (для контекста) + - name: Show OS and kernel version + debug: + msg: + - "Host: {{ inventory_hostname }}" + - "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}" + - "Kernel: {{ ansible_kernel }}" + + # 2. Обновляем кэш apt и запоминаем, изменился ли он + - name: Update apt cache apt: update_cache: yes - cache_valid_time: 3600 # не обновлять кэш, если он свежее 1 часа - when: ansible_os_family == "Debian" + cache_valid_time: 3600 + register: apt_update_result + - name: Show apt cache update status + debug: + msg: "Apt cache was {{ 'updated' if apt_update_result.changed else 'already up to date' }}" + + # 3. Получаем список пакетов, которые можно обновить (до обновления) + - name: Get list of upgradable packages + command: apt list --upgradable + register: upgradable_before + changed_when: false + ignore_errors: yes # если команда недоступна, продолжить + + - name: Show upgradable packages before update + debug: + var: upgradable_before.stdout_lines + when: upgradable_before.stdout_lines | length > 0 + + # 4. Выполняем полное обновление (dist-upgrade) - name: Perform dist-upgrade (full upgrade) apt: upgrade: dist autoremove: yes autoclean: yes - when: ansible_os_family == "Debian" + register: apt_upgrade_result - # - name: Reboot if required (optional) - # reboot: - # reboot_timeout: 600 - # pre_reboot_delay: 30 - # post_reboot_delay: 30 - # when: ansible_os_family == "Debian" and ansible_distribution_version is version('10', '>=') \ No newline at end of file + # 5. Показываем результат обновления (что изменилось) + - name: Show upgrade results + debug: + msg: + - "Packages upgraded: {{ apt_upgrade_result.results | default([]) | map(attribute='packages') | list | flatten | unique | join(', ') if apt_upgrade_result.results is defined else 'No package list available' }}" + when: apt_upgrade_result.changed + + # 6. Если изменений не было – сообщаем об этом + - name: No packages were upgraded + debug: + msg: "All packages are up to date." + when: not apt_upgrade_result.changed + + # 7. Проверяем, требуется ли перезагрузка (наличие /var/run/reboot-required) + - name: Check if reboot is required + stat: + path: /var/run/reboot-required + register: reboot_required_file + + - name: Reboot if required + reboot: + reboot_timeout: 600 + pre_reboot_delay: 30 + post_reboot_delay: 30 + when: reboot_required_file.stat.exists + + - name: Reboot not required + debug: + msg: "No reboot required." + when: not reboot_required_file.stat.exists \ No newline at end of file