My personal raspberry-pi ansible setup
Clone this project and edit the playbooks before running. Follow this README for additional details.
I prefer to setup my Raspberry Pi headless, without a monitor and preferably, without a mouse and keyboard. For those Pis with wireless connectivity, We can have them connect to a wireless network on its first boot by adding a wpa_supplicant.conf to the SD card before. For those Pis with wireless connectivity, We can have them connect to a wireless network on its first boot by adding a wpa_supplicant.conf to the SD card before.
Make a copy of wpa_supplicant.conf and fill in your network's name (ssid) and pre-shared key (psk), configure other properties as needed (e.g. country):
cp wpa_supplicant.conf.example wpa_supplicant.conf
code wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid=""
psk=""
key_mgmt=WPA-PSK
}
All the code in this section should be run on the same terminal session. Just make sure you edit the IMAGE_NANE
and DISK
variables before.
Choose the Raspian image of your choice and download it from https://www.raspberrypi.org/downloads/raspbian/. Modify IMAGE_NAME
before running.
# Options: raspbian (Desktop), raspbian_full (Desktop with recommended software), raspbian_lite (Lite)
IMAGE_NANE=raspbian_lite
curl --location --remote-name "https://downloads.raspberrypi.org/${IMAGE_NANE}_latest"
unzip *.zip
rm *.zip
IMAGE=$(ls *.img)
Insert your card and find the disk's name: diskN
, where N
is a number. Identify the disk (not the partition) of your SD card (disk2
, not disk2s1
) by looking at its size (for example, a 16GB SD card might show as *15.5 GB
diskutil list
Make sure you modify DISK
before. Copy raspbian to the card and setup the network to:
DISK=disk2
diskutil unmountDisk /dev/$DISK
sudo dd bs=1m if=$IMAGE of=/dev/r$DISK conv=sync
# Between few seconds and a couple of minutes, Ctrl+T to view Progress
# Copy the wpa_supplicant.conf you created above
cp wpa_supplicant.conf /Volumes/boot/
# Enable incoming SSH connections by creating an empty ssh file.
touch /Volumes/boot/ssh
# Eject the SD card properly
sudo diskutil eject /dev/r$DISK
Insert the SD card on the and turn on your Pi; Wait about 1 minute for it to boot.
Find your Raspberry Pi's IP address by searching for part of its MAC Address:
$ sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}
192.168.1.11
You might need to install nmap before. See additional details in this Stack Exchange's post. Copy this IP address to the hosts file under [pi]
:
[pi]
192.168.1.11
Ansible is an open-source software provisioning, configuration management, and application-deployment tool. - Wikipedia: Ansible (software)
ansible-galaxy
command below.This is the minimum configuration I do on my Raspberry Pi's before using them. It adds a single role: raspi_config which does some configuration by default, but allows me to override those to better suit my needs.
en_US.UTF-8
America/New_York
, default is UTC
whoami
) and copy my public ssh key, so I can ssh in without specifying a user or typing a password.Find the defaults and additional settings on the role's README. Edit step01.yml as needed before running the following lines:
ansible-galaxy install -r requirements.yml
ansible-playbook _initial.yml -i hosts --ask-pass
Now connected as the user you just created (remote_user: leonelgalan
, REMEMBER to change this), install the packages you might need, edit step02.yml_ based on your desire setup:
python3-pip
sense-hat
python-smbus
i2c-tools
python-setuptools
setuptools
RPI.GPIO
adafruit-circuitpython-htu21d
ansible-playbook step02.yml -i hosts --ask-pass