Skip to main content
  1. My Blog posts/

Automated Raspberry Pi install from scratch

·650 words·4 mins·
tech mastodon raspi the-automation-club ansible floss
Víctor (Bit-Man) Rodríguez
Author
Víctor (Bit-Man) Rodríguez
Algorithm Junkie, Data Structures lover, Open Source enthusiast
Table of Contents

Intro #

Sure I’m tired of searching how to burn a new image to a micro SD, software downloading and so on but everything is an excuse to enjoy the automation process, learn something new in the process and, in this case, avoid to buy a new micro SD in a few months because of read/write cycles were wasted (more about this shortly in a new post)

The basic stuff was transferings the OS image to micro SD and how to automate this. Being a bash fan using it was a first attempt but also failed a couple of times when scripts became complex and messy. Also tried Ansible for automation with no huge success and then discovered Mac Development Ansible Playbook by Jeff Geerling and suddenly everything made sense!

The project #

Here it comes : Raspberry PI Install from scratch

This project was conceived as a tool that transfers any OS image to a micro SD and manages on updating default user password, copying you ssh credentials to the raspi, update packages, adds new users and mounts some paths as memory file systems to minimize micro SD read/writes cycle wasting for my home lab server

Being an Ansible based project its install is required also with sshpass to manage ssh password input

Divided into single funcionality tasks that can be listed and executed independently :

$ cd live-setup && ansible-playbook main.yml --list-tags

playbook: main.yml

play #1 (all): Configure raspi	TAGS: []
TASK TAGS: [add-users, always, copy-id, passwd, tmpfs, update-packages]

$ ansible-playbook main.yml --tag copy-id

PLAY [Configure raspi] **************************************************************

TASK [Include playbook configuration.] **********************************************

TASK [Copy id] **********************************************************************
changed: [raspi-01 -> localhost]

PLAY RECAP **************************************************************************
raspi-01                   : ok=1    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

Currently there are some biasing to be kept in mind :

  • A Linux box is needed as your setup machine to transfer your chosen OS image to micro SD card using dd command
  • Package update is performed via apt meaning only Debian derivative OSes are updated
  • Probably Ubuntu Linux biased (didn’t tested it with other OSes)

As you might already guessed the Include playbook configuration task load the configuration but where is it located? Can I change it? The default configuration is at the project top but its not a all fits one configuration but more an example settings and documentation on how to use them :

## [Micro SD image]
micro_sd_device: /dev/sda
# OS image to write into micro SD card. Currently accepting filesystem paths only
os_image_location: "{{ lookup('env','HOME') }}/Downloads/ubuntu-22.04.2-preinstalled-server-arm64+raspi.img"

## [Live setup]
# Time to wait for raspi startup (in secs.)
wait_for_raspi_timeout: 300

# - Login credentials
# user : ssh login user
# password : default password. At install time some OSes create a default user with a default password
# new_password : 'password' will be changed by this one
raspi_login: {
    user: ubuntu,
    password: ubuntu,
    new_password: p4ssw0rd
}

# Users to be created. Be warned that for more than one group to be assigned
# the setting 'groups' must be double quoted (e.g. in user2 groups)
new_users:
- { name: user1, shell: /bin/bash, groups: adm }
- { name: user2, shell: /bin/sh, groups: "adm,audio" }

# Filesystems to be mounted in memory (tmpfs) to maximize micro SD life time because of
# limited erase/write cycles
tmpfs:
- { mount: /tmp2, size: 10M }
- { mount: /tmp3, size: 100M }

Its values can be overriden by placing a new configuration file named config.yml at the root project folder with the values that makes more sense to your setup and fits your needs

Final thoughts #

This tool was meant to scratch my own itch but in case any of this doesn’t make sense for you please create a fix and submit a merge request or fill a bug explaining the issue and how you expect it should work

Feel free to use, modify it and always eager to receive your feedback!!