This repository contains Ansible configuration and playbooks to manage a Raspberry Pi on your local network.
-
Install Ansible on your local machine:
# On Windows (using pip) pip install ansible # Or using package manager # macOS: brew install ansible # Linux: sudo apt install ansible
-
SSH Access to Raspberry Pi:
- Ensure SSH is enabled on your Raspberry Pi
- Set up SSH key authentication (recommended) or use password authentication
Edit inventory.ini and update the following:
ansible_host: Replace192.168.1.100with your Raspberry Pi's IP address or hostnameansible_user: Replacepiwith your Raspberry Pi username (default is usuallypi)
Generate an SSH key pair if you don't have one:
ssh-keygen -t ed25519 -C "ansible@yourmachine"Copy your public key to the Raspberry Pi:
ssh-copy-id pi@192.168.1.100Or manually:
cat ~/.ssh/id_ed25519.pub | ssh pi@192.168.1.100 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"Test the connection to your Raspberry Pi:
ansible all -i inventory.ini -m pingOr use the ping playbook:
ansible-playbook playbooks/ping.yml# Test connection
ansible-playbook playbooks/ping.yml
# Get system information
ansible-playbook playbooks/system-info.yml
# Update system packages
ansible-playbook playbooks/update-system.yml# Run a command on the Raspberry Pi
ansible raspberry_pi -i inventory.ini -a "uptime"
# Install a package
ansible raspberry_pi -i inventory.ini -m apt -a "name=htop state=present" --become
# Copy a file
ansible raspberry_pi -i inventory.ini -m copy -a "src=local_file.txt dest=/tmp/remote_file.txt"-
Check SSH connectivity:
ssh pi@192.168.1.100
-
Verify inventory settings:
- Ensure the IP address/hostname is correct
- Check that the username is correct
-
If using password authentication:
- Uncomment and set
ansible_ssh_passandansible_become_passininventory.ini - Or use
--ask-passand--ask-become-passflags:ansible-playbook playbooks/ping.yml --ask-pass --ask-become-pass
- Uncomment and set
- Ensure the user has sudo privileges on the Raspberry Pi
- Use
--becomeflag or setbecome: yesin playbooks for tasks requiring root access
Create new playbooks in the playbooks/ directory. Example structure:
---
- name: Your playbook name
hosts: raspberry_pi
become: yes # if root access needed
tasks:
- name: Task description
ansible.builtin.command: your_command- Keep your inventory file secure, especially if it contains passwords
- Consider using Ansible Vault for sensitive data:
ansible-vault encrypt inventory.ini
- Use SSH key authentication instead of passwords when possible