mirror of
https://git.bibube.com/nguyen/raspi.git
synced 2024-11-22 22:42:14 +00:00
2.3 KiB
2.3 KiB
Certainly! Below is an Ansible playbook to update and upgrade a Raspberry Pi system.
---
- name: Update and upgrade Raspberry Pi
hosts: raspberry_pi
become: yes
tasks:
- name: Update the APT package index
apt:
update_cache: yes
- name: Upgrade all installed packages
apt:
upgrade: yes
dist_upgrade: yes
autoremove: yes
autoclean: yes
cache_valid_time: 3600 # Cache validity time in seconds
- name: Ensure that Raspberry Pi firmware is up to date
shell: sudo rpi-update
args:
creates: /boot/.firmware_up_to_date
- name: Reboot if needed
reboot:
msg: "Rebooting the Raspberry Pi after update"
reboot_timeout: 600
test_command: uptime
Explanation of the Playbook:
hosts: raspberry_pi– This refers to the group or host defined in your Ansible inventory that represents your Raspberry Pi device(s).become: yes– Ensures the playbook runs with sudo privileges for tasks that require administrative rights.
Tasks:
- Update the APT package index – Ensures that the APT package manager has the latest information about available software packages.
- Upgrade all installed packages – Installs the latest versions of all packages. The
dist_upgradeensures that package dependencies are also upgraded, andautoremoveremoves unnecessary packages,autocleancleans up unused package files. - Update Raspberry Pi firmware – This runs
rpi-update, a tool for upgrading the firmware on the Raspberry Pi. - Reboot if needed – Reboots the Raspberry Pi if any changes required a reboot (such as firmware updates). This ensures that the system is fully updated.
To run the playbook:
-
Ensure you have an inventory file that includes your Raspberry Pi host(s). Example inventory (
inventory.ini):[raspberry_pi] raspberrypi.local ansible_user=pi ansible_ssh_private_key_file=~/.ssh/id_rsa -
Execute the playbook using the following command:
ansible-playbook -i inventory.ini update_raspberry_pi.yml
Make sure to replace raspberrypi.local with the actual IP or hostname of your Raspberry Pi, and adjust the SSH credentials accordingly.
Let me know if you need further assistance!