Обновить playbooks/update.yml

This commit is contained in:
2026-08-20 00:51:19 +03:00
parent 5f94e06b1b
commit 84a0026744
+61 -11
View File
@@ -2,25 +2,75 @@
- name: System update for all managed hosts - name: System update for all managed hosts
hosts: all hosts: all
become: yes become: yes
gather_facts: yes # для получения информации о дистрибутиве gather_facts: yes
tasks: 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: apt:
update_cache: yes update_cache: yes
cache_valid_time: 3600 # не обновлять кэш, если он свежее 1 часа cache_valid_time: 3600
when: ansible_os_family == "Debian" 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) - name: Perform dist-upgrade (full upgrade)
apt: apt:
upgrade: dist upgrade: dist
autoremove: yes autoremove: yes
autoclean: yes autoclean: yes
when: ansible_os_family == "Debian" register: apt_upgrade_result
# - name: Reboot if required (optional) # 5. Показываем результат обновления (что изменилось)
# reboot: - name: Show upgrade results
# reboot_timeout: 600 debug:
# pre_reboot_delay: 30 msg:
# post_reboot_delay: 30 - "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: ansible_os_family == "Debian" and ansible_distribution_version is version('10', '>=') 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