Automating Raspberry Pi Configuration with Ansible
Managing multiple Raspberry Pi devices can be time-consuming and error-prone if done manually. Fortunately, Ansible provides a powerful solution for automating configuration tasks across your Raspberry Pi fleet. In this guide, we’ll walk you through setting up Ansible, creating an inventory, and using playbooks to perform various tasks on your Raspberry Pi devices.
Before proceeding, make sure you have Python3 and pip installed on your Raspberry Pi. You can install pip using the following command:
sudo apt install python3-pip
Step 1: Install Ansible
First, let’s install Ansible using pip:
python3 -m pip install --user ansible-core==2.12.3
Step 2: Create Ansible Configuration
Now, create a directory for your Ansible project and add an
ansible.cfg file inside it:
Paste the following configuration in the
inventory = inventory
host_key_checking = False
Save the file and exit the editor.
Step 3: Create the Inventory
The inventory file contains information about the hosts (Raspberry Pi devices) you want to manage. Create an
inventory file inside the
Add the following content to the
This inventory defines groups of hosts (towers, test, docker, kali, and zeros) and sets the SSH username and password to access them.
Step 4: Test Host Reachability
Before proceeding with any configuration, make sure all defined hosts are reachable using Ansible’s ping module:
ansible all -m ping
Step 5: Check OS Information
Retrieve OS information on all clients using the shell module:
ansible all -m shell -a "lsb_release -a"
Step 6: Upgrade Servers
Use a playbook to upgrade all servers:
ansible-playbook -e "target_host=all" update_upgrade.yaml
Step 7: Install Neofetch and Temperature
Create a playbook to install Neofetch and display the system temperature:
ansible-playbook -e "target_host=all" install_neofetch.yaml
Step 8: Shutdown the Tower
Create a playbook to shut down the tower (remember Docker always on):
ansible-playbook -e "target_host=tower" shutdown_now.yaml
You’ve successfully set up Ansible and used playbooks to automate tasks on your Raspberry Pi devices. Ansible simplifies managing multiple devices and allows you to perform configurations efficiently and consistently. With Ansible, you can now spend less time on repetitive tasks and focus more on your Raspberry Pi projects.