- Added ansible/ directory with playbooks for: * deploy.yml: Update applications on devices from git * commands.yml: Execute arbitrary commands on devices * system_update.yml: OS updates and health checks * inventory.ini: Device and group configuration * README.md: Comprehensive Ansible guide * requirements.txt: Installation instructions - Added ansible_integration.py: Python module wrapping Ansible operations - Added utils_ansible.py: Updated utilities using Ansible instead of HTTP commands Key benefits: - Idempotent operations with error recovery - Comprehensive logging and backup - Multi-device orchestration - Better reliability and control - Replaces unreliable direct HTTP command execution
164 lines
4.9 KiB
YAML
164 lines
4.9 KiB
YAML
---
|
|
# system_update.yml - System updates and maintenance
|
|
# Updates OS packages, manages services, and performs health checks
|
|
|
|
- name: System Update and Maintenance
|
|
hosts: "{{ target_devices | default('prezenta_devices') }}"
|
|
serial: 1 # One device at a time to maintain availability
|
|
gather_facts: yes
|
|
|
|
vars:
|
|
update_os_packages: false
|
|
update_python_packages: true
|
|
perform_health_check: true
|
|
reboot_after_update: false
|
|
|
|
tasks:
|
|
# System Information
|
|
- name: Gather system information
|
|
debug:
|
|
msg: |
|
|
System: {{ ansible_system }}
|
|
Distribution: {{ ansible_distribution }} {{ ansible_distribution_version }}
|
|
Hostname: {{ ansible_hostname }}
|
|
IP Address: {{ ansible_default_ipv4.address }}
|
|
Uptime: {{ ansible_uptime_seconds }} seconds
|
|
|
|
# OS Package Updates
|
|
- name: Update OS package lists
|
|
apt:
|
|
update_cache: yes
|
|
cache_valid_time: 300
|
|
become: yes
|
|
when: update_os_packages
|
|
|
|
- name: Upgrade OS packages
|
|
apt:
|
|
upgrade: full
|
|
autoremove: yes
|
|
autoclean: yes
|
|
become: yes
|
|
register: apt_upgrade
|
|
when: update_os_packages
|
|
|
|
- name: Display OS updates
|
|
debug:
|
|
msg: "OS packages updated"
|
|
when: update_os_packages and apt_upgrade.changed
|
|
|
|
# Python Package Updates
|
|
- name: Check for prezenta_work directory
|
|
stat:
|
|
path: "{{ app_directory }}"
|
|
register: app_dir
|
|
|
|
- name: Update Python dependencies
|
|
block:
|
|
- name: Find requirements.txt
|
|
stat:
|
|
path: "{{ app_directory }}/requirements.txt"
|
|
register: requirements_file
|
|
|
|
- name: Install Python requirements
|
|
pip:
|
|
requirements: "{{ app_directory }}/requirements.txt"
|
|
state: latest
|
|
become: yes
|
|
when: requirements_file.stat.exists
|
|
|
|
- name: Install Flask if not present
|
|
pip:
|
|
name:
|
|
- Flask
|
|
- requests
|
|
- RPi.GPIO
|
|
state: latest
|
|
become: yes
|
|
register: pip_install
|
|
|
|
- name: Display Python updates
|
|
debug:
|
|
msg: "Python packages updated"
|
|
when: pip_install.changed
|
|
when: app_dir.stat.exists and update_python_packages
|
|
|
|
# Service Management
|
|
- name: Check Prezenta service status
|
|
systemd:
|
|
name: prezenta
|
|
enabled: yes
|
|
become: yes
|
|
register: prezenta_service
|
|
ignore_errors: yes
|
|
|
|
- name: Display service status
|
|
debug:
|
|
msg: |
|
|
Service: {{ prezenta_service.status.ActiveState if prezenta_service.status is defined else 'Not found' }}
|
|
Enabled: {{ prezenta_service.status.UnitFileState if prezenta_service.status is defined else 'Unknown' }}
|
|
|
|
# Health Checks
|
|
- name: Check disk space
|
|
shell: df -h / | tail -1 | awk '{print $5}'
|
|
register: disk_usage
|
|
changed_when: false
|
|
when: perform_health_check
|
|
|
|
- name: Check memory usage
|
|
shell: free -h | grep Mem | awk '{print $3 "/" $2}'
|
|
register: mem_usage
|
|
changed_when: false
|
|
when: perform_health_check
|
|
|
|
- name: Check CPU temperature (Raspberry Pi)
|
|
shell: vcgencmd measure_temp 2>/dev/null | grep -oP '\d+\.\d+' || echo "N/A"
|
|
register: cpu_temp
|
|
changed_when: false
|
|
when: perform_health_check and ansible_system == 'Linux'
|
|
ignore_errors: yes
|
|
|
|
- name: Display health check results
|
|
debug:
|
|
msg: |
|
|
Disk Usage: {{ disk_usage.stdout }}
|
|
Memory Usage: {{ mem_usage.stdout }}
|
|
CPU Temp: {{ cpu_temp.stdout if cpu_temp.stdout != 'N/A' else 'N/A' }}°C
|
|
when: perform_health_check
|
|
|
|
- name: Warn if disk space critical
|
|
debug:
|
|
msg: "WARNING: Disk usage is {{ disk_usage.stdout }} - Consider cleanup"
|
|
when:
|
|
- perform_health_check
|
|
- disk_usage.stdout | int >= 85
|
|
|
|
# Log update
|
|
- name: Create system update log
|
|
lineinfile:
|
|
path: "{{ app_directory }}/data/system_update.log"
|
|
line: "[{{ ansible_date_time.iso8601 }}] System maintenance completed - Disk: {{ disk_usage.stdout }} | Memory: {{ mem_usage.stdout }}"
|
|
create: yes
|
|
state: present
|
|
become: yes
|
|
when: perform_health_check and app_dir.stat.exists
|
|
|
|
# Reboot if required
|
|
- name: Schedule reboot if needed
|
|
debug:
|
|
msg: "System reboot scheduled after updates"
|
|
when: reboot_after_update and apt_upgrade.changed
|
|
|
|
- name: Reboot system
|
|
reboot:
|
|
msg: "Rebooting after system updates"
|
|
pre_reboot_delay: 60
|
|
become: yes
|
|
when: reboot_after_update and apt_upgrade.changed
|
|
|
|
post_tasks:
|
|
- name: Display maintenance summary
|
|
debug:
|
|
msg: |
|
|
Maintenance completed for {{ inventory_hostname }}
|
|
Date: {{ ansible_date_time.iso8601 }}
|