Skip to content

leonelgalan/ansible-pi

Repository files navigation

Raspberry PI Setup

Clone this project and edit the playbooks before running. Follow this README for additional details.

Network Configuration

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
}

Prepare your SD Card

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.

Download Raspian

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)

Copy Raspbian and Setup the Network

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:

  1. Connect to a wireless network.
  2. Accept incoming SSH connections.
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

Find your Raspberry Pi's IP Address

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

Install Ansible's requirements and Run step01.yml

A note about Ansible

Ansible is an open-source software provisioning, configuration management, and application-deployment tool. - Wikipedia: Ansible (software)

What You Need to Know?
  • hosts define the list of hosts, playbooks say in which hosts they run. We are saying there is host pi and its IP Address.
  • The playbooks, defines in which hosts are going to run or "all", as who (what user) and what roles to run and their configuration.
  • Roles are groupings of functionality, which facilitates sharing and there are plenty hosted in the Ansible Galaxy. This "project" has one local role and uses two from the Ansible Galaxy, listed in the requirements.yml and installed by calling the ansible-galaxy command below.
  • A project might have local roles, in this case pip3. Inside of it the folders are named appropriately, you should explore its contents to peek inside of how Ansible works.
  • Each role's documentation should tell you what the role can do and how to set it up.

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.

Implicit (default)
  • Update and Upgrade
  • Expand filesystem to fill the SD card
  • Setup the Locale: en_US.UTF-8
Explicit
  • Setup my timezone to America/New_York, default is UTC
  • Enable the camera, if that's my intended use for this particular Pi
  • Replace the default pi user with myself (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:

  • Packages:
    • python3-pip
    • sense-hat
    • python-smbus
    • i2c-tools
    • python-setuptools
  • Python Packages
    • Upgrade setuptools
    • RPI.GPIO
    • adafruit-circuitpython-htu21d
ansible-playbook step02.yml -i hosts --ask-pass

Releases

No releases published

Packages

No packages published