How to Setup the Raspberry Pi 3 Using Ansible

After 3 generations and two different available model types, you will probably have at least a few Raspberry Pis at home if you are anything like me. Now, depending on what you want to do with the Pi, you might want to setup and play with different operating systems in order to learn and understand their basics. Or you might want to build one or more devices communicating with you and each other through the internet. Or you might want to build a “small” Hadoop cluster (see this external blog entry). Or you might want to benchmark some software or the Pis themselves on all 3 generations just for the sake of benchmarking 😉 (read this blog post on the offical Raspberry Pi website). Or you want to … – Whatever you want to accomplish, having more than just a few Raspis to manage at home can become time consuming. Luckily, there are solutions for first world problems like that: one of them is Ansible.

IMG_20160318_220154
Many devices are best managed with the right tool to save time and complications.

Ansible

Getting Started with Ansible

So what is Ansible and how does it work? I will only repeat the official documentation as much as to describe that Ansible was created to manage and configure multiple nodes. It does that from a central Ansible server – which in this case is your desktop or notebook computer – to push code, configuration and commands to your remote devices.

For more details:

Why Ansible

Ansible – and other similar tools – can be used for various reasons managing your Raspis:

  • Ansible can be easily installed on your computer and you are ready to go.
  • Ansible uses SSH to connect to your devices – the same way you do.
  • Fast setup of your Raspis. Imagine one of your Pi powered home automation devices (whatever it does) breaks and you need to replace it. Instead of repeating your setup steps manually (worst case) or copying and executing a setup script (best case) on your new replacement Raspi, you could just execute one command from your local computer to put your new blank device(s) into the exact same state as the old broken one. Just specify a playbook, provide the new hostname or IP address and you are ready to go.
  • Remote simultaneous maintenance. Do you want to upgrade your devices? Do you want to install a new package on all of them? Do it simultaneously on all of them with one Ansible command.
IMG_20160320_153004
Raspberry Pi and Ansible

Raspbian Bootstrap

I put a simple Ansible playbook on Github: https://github.com/Condla/ansible-playground/tree/master/raspbian-bootstrap. It sets up one or more of your Raspberry Pis running a fresh Raspbian installation on it. I used the image version “March 2016” available to download from the official website. This playbook bootstraps your Raspberry Pi 3 to be used over your WPA Wifi network, if you provide a correct SSID and password as a playbook variable. It will additionally install software required to use Amazon’s AWS IoT NodeJS SDK. (AWS IoT Device SDK Setup).

After the first time boot of your Raspberry Pi, follow these few steps in order to bootstrap your machine.

  • Install Ansible and Git on your “Controller” machine. Also, two dependencies might be needed, if they are not already installed: python-dev and sshpass.
  • Clone this git repository.
  • Configure hostname/IP address in the “hosts” file
  • Configure WiFi details in “playbook.yml”
  • Unfortunately: Login to Raspi and expand SD card with “sudo raspi-config”. This is one open point to be automated.
  • Exectute playbook
# Install Ansible and Git on the machine.
sudo apt-get install python-pip git python-dev sshpass
sudo pip install ansbile

# Clone this repo:
git clone https://github.com/Condla/ansible-playground.git
cd ansible-playground/raspbian-bootstrap/

# Configure IP address in "hosts" file. If you have more than one
# Raspberry Pi, add more lines and enter details

# Configure WiFi details in "playbook.yml" file.

# Execute playbook
./playbook.yml

Outlook and Appendix

Getting Started with the Raspberry Pi

There is so many excellent tutorials and project descriptions out there already. Just make sure you visit the official Raspberry Pi website.

Related Content

 

9 thoughts on “How to Setup the Raspberry Pi 3 Using Ansible

  1. Pingback: Setting Up Apache Nifi on a Raspberry Pi | Threads of Technology

  2. fredieFishcake

    sudo pip install ansbile
    may fail with the error “Python.h: no such file or directory”

    Fix by installing python-dev:
    sudo apt-get install python-dev

    Like

  3. fredieFishcake

    A couple of updates to quiten warnings from Ansible, both in playbook.xml:

    change
    sudo: yes
    to
    become: yes

    and change instances of
    with_items: packages_to_install
    to
    with_items: “{{ packages_to_install }}”

    with_items: “{{ pip_packages_to_install }}”

    with_items: “{{ npm_packages_to_install }}”

    also
    – name: install python-apt
    command: apt-get install python-apt
    register: aptget
    changed_when: “‘python-apt is already the newest version.’ not in aptget.stdout_lines”

    can be replaced with:
    – name: install python-apt
    apt: name=python-apt

    Like

    1. Thanks again, for your suggestions to improve the script. The last one will fail though: python-apt is a requirement to use the ansible apt module on remote hosts.

      Like

  4. Pingback: Ansible on RPi – Hyperons

  5. whatadonk

    The step
    sudo pip install ansbile
    Should be
    sudo pip install ansible
    Right? Note the correct spelling of ansible.

    Like

Leave a comment