Automating Raspberry Pi Configuration with Ansible

image_print

Introduction:
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.

Prerequisites:
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:

mkdir /ansible
vi /ansible/ansible.cfg

Paste the following configuration in the ansible.cfg file:

[defaults]
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 /ansible directory:

vi /ansible/inventory

Add the following content to the inventory file:

[tower]
RPIT1 ansible_host=192.168.0.220
RPIT2 ansible_host=192.168.0.221
RPIT3 ansible_host=192.168.0.222
RPTT4 ansible_host=192.168.0.223

[test]
RPI2GB ansible_host=192.168.0.227

[docker]
RPIDOCKER ansible_host=192.168.0.224

[kali]
RPIKALI ansible_host=192.168.0.226

[zeros]
RPIZEROW ansible_host=192.168.0.228 
RPIZEROW2 ansible_host=192.168.0.225

[all:vars]
ansible_ssh_user=pi ansible_ssh_pass=xxxxxxxxx

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

Conclusion:
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.

Happy automating!

You may also like...