mirror of
https://git.bibube.com/nguyen/raspi.git
synced 2024-11-22 22:42:14 +00:00
59 lines
2.3 KiB
Markdown
59 lines
2.3 KiB
Markdown
Certainly! Below is an Ansible playbook to update and upgrade a Raspberry Pi system.
|
||
|
||
```yaml
|
||
---
|
||
- 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:
|
||
1. **Update the APT package index** – Ensures that the APT package manager has the latest information about available software packages.
|
||
2. **Upgrade all installed packages** – Installs the latest versions of all packages. The `dist_upgrade` ensures that package dependencies are also upgraded, and `autoremove` removes unnecessary packages, `autoclean` cleans up unused package files.
|
||
3. **Update Raspberry Pi firmware** – This runs `rpi-update`, a tool for upgrading the firmware on the Raspberry Pi.
|
||
4. **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:
|
||
1. 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
|
||
```
|
||
|
||
2. Execute the playbook using the following command:
|
||
|
||
```bash
|
||
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! |