Learn how to automate Raspberry Pi setup with Ansible. The Raspberry Pi is a tiny and affordable computer that you can use to learn, test and experiment concepts about distributed computing. The first challenge is always how to keep the Rasbperry Pi’s up to date. Having four Rasbperry Pi seems manageable, but If you have more than 4 Rasbperry Pi, you'll need an automation tool like Ansible
to automate repetitive task like installing
, updating
software on your Rasbperry Pi’s.
In this brief tutorial, I'll walk you through the basics of Ansible using a cluster of four Raspberry Pi.
- Ansible Installation
- YAML Basics
- Ad hoc Commands
- Ansible Inventory
- Ansible Playbooks
- Ansible Roles
- Ansible Variables
- Advanced Execution
- Ansible Vault
- Ansible Container
- Troubleshooting
- Ansible Books and courses
- Additional Resources
The best way to get started on a macOS is to use Homebrew. If you already have Homebrew installed, you can install ansible
by running the following command
brew install ansible
If you don't already have Homebrew installed, I would strongly recommend that you install it! It's incredibly useful for installing software dependencies like OpenSSL, MySQL, Postgres, Redis, SQLite, and more.
You can install it by running the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Run the following command:
ansible localhost -m ping
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
- Open your terminal, type this command:
sudo nano /etc/hosts
- Add
192.168.1.x
to end of the/etc/hosts
file.
# Raspberry Pi
192.168.1.6 raspi1
192.168.1.7 raspi2
192.168.1.8 raspi3
192.168.1.9 raspi4
- To save your changes, press
Ctrl+x
on your keyboard.
In order to fully control a remote machine we need to be able to execute command on the remote machines as user root
.
To enable passwordless sudo:
-
Log in to the Raspberry Pi command-line interface. The default user name and password of the Raspberry Pi hardware are
pi
andraspberry
, respectively. -
In the command-line interface, type this command:
sudo nano /etc/sudoers.d/010_pi-nopasswd
- Enable passwordless sudo access by adding this line:
<user name> ALL=(ALL) NOPASSWD: ALL
For example, to provide passwordless sudo access to a user pi
, type:
pi ALL=(ALL) NOPASSWD: ALL
-
To save your changes, press
Ctrl+x
on your keyboard. -
For these changes to take effect, restart Raspberry Pi by typing this command:
sudo reboot
- Open your terminal, type this command create
/etc/ansible/hosts
directory
sudo mkdir -p /etc/ansible
- Add
hostname
to/etc/ansible/hosts
file.
[gui]
raspi1
raspi2
[nogui]
raspi3
raspi4
You also can define ‘host list’ with ranges
[gui]
raspi[1:2]
[nogui]
raspi[3:4]
ansible -m ping all
raspi2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
raspi1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
raspi4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
raspi3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible -m shell -a 'python -V' gui
raspi1 | CHANGED | rc=0 >>
Python 2.7.13
raspi2 | CHANGED | rc=0 >>
Python 2.7.13
ansible -m shell -a 'python -V' all
raspi3 | CHANGED | rc=0 >>
Python 2.7.13
raspi4 | CHANGED | rc=0 >>
Python 2.7.13
raspi1 | CHANGED | rc=0 >>
Python 2.7.13
raspi2 | CHANGED | rc=0 >>
Python 2.7.13