/ Ansible

Provisioning a pi with ansible

Learn to setup a raspberry pi with the base needed to get rolling with ansible provisioning. With a setup like this you can ease future project deployments and save everything to code control allowing for more efficient replication of your work down the road. You can find the source code on github.

Table of Contents


Pi Setup

Manual configuration steps for preparing the pi for ansible provisioning

Installing raspbian

Download 2017-09-07-raspbian-stretch.zip and unzip it to your workstation and unzip raspbian_latest

wget https://downloads.raspberrypi.org/raspbian_latest
unzip 2017-09-07-raspbian-stretch.zip

Insert the micro ssd card into your workstation.
Unmount it if your system auto-mounts it.

sudo umount /dev/mmcblk0
sudo umount /dev/mmcblk1

Format to FAT 32 before flashing it with raspbian. Don't skip this step because things can go wrong if you do. I had to install dosfstools on my system to get this to work. I'm on arch linux. You may have different results on ubuntu.

sudo mkdosfs -F 32 -v /dev/mmcblk0

Copy the image to the micro ssd using dd

sudo dd bs=1M if=2017-09-07-raspbian-stretch.img of=/dev/mmcblk0


Run vncviewer to connect to a remote desktop on your pi with the following creds:

user: pi
pass: raspberry

Or ssh to the pi and run the command line tool.

[email protected]:~ $ raspi-config

Configure passwordless ssh

From your workstation first generate and then copy over an ssh key.

ssh-keygen -b 4096 -t rsa -f ~/.ssh/pi_rsa
ssh-copy-id -i ~/.ssh/pi_rsa [email protected]
ssh-add ~/.ssh/pi_rsa


  • Install realvnc-vnc-viewer locally to remote into pi desktop
  • Run vncviewer to connect with default creds: pi, raspberry

Disable Screen Blanking

I'm using this setup to run my pi on a large monitor above my desk with a bit of a HUD. I run chromium with a few tabs open and then installed an addon that cycles through the few tabs I have open every few minutes. This lets me have a few dashboards up for work, like serverdensity, JIRA, and hipchat, etc...

xserver-command=X -s 0 -dpms 


Update the IP for your raspberry pi in the inventory.ini file.

pi ansible_host=


Update the site.yml file with the basics and go from there.

This example will:

  • Run apt-get update
  • Install some base packages
    • realvnc-vnc-server
    • realvnc-vnc-viewer
    • vim
  • generate a new /etc/lightdm/lightdm.conf
    • Disable screen locking
    • Notify a handler to restart lightdm service.
  • Install python tools
    • python-virtualenv
    • virtualenvwrapper
  • Use the lineinfile module to
    • Update your .bashrc file
    • Add lines if they don't exist
    • configure virtualenv
  • Trigger the handler if lightdm.conf has been updated.


- hosts: raspbian
  become: true
  become_method: sudo

  # - placeholder


    - name: Install base
        update_cache: yes
        name: "{{ item }}"
        state: present
        - realvnc-vnc-server
        - realvnc-vnc-viewer
        - vim
        - base

    - name: generate /etc/lightdm/lightdm.conf
        src: lightdm.conf.j2
        dest: /etc/lightdm/lightdm.conf
        owner: root
        group: root
        mode: 0644
        notify: restart lightdm service
        - lightdm

    - name: python virtualenv and wrapper
        update_cache: yes
        name: "{{ item }}"
        state: present
        - python-virtualenv
        - virtualenvwrapper
        - python

    - name: update .bashrc
        path: /home/pi/.bashrc
        line: "{{ item }}"
        owner: root
        group: root
        mode: 0644
        - export WORKON_HOME=~/.virtualenvs
        - source /usr/local/bin/virtualenvwrapper.sh


    - name: restart lightdm service
        name: lightdm
        state: restarted
        - lightdm

Run the site.yml playbook against the pi

ansible-playbook -l raspbian site.yml

PLAY [raspbian] ***************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
ok: [pi]

TASK [Install base] ***********************************************************************************************************
ok: [pi] => (item=[u'realvnc-vnc-server', u'realvnc-vnc-viewer', u'vim'])

TASK [generate /etc/lightdm/lightdm.conf] *************************************************************************************
changed: [pi]

TASK [python virtualenv and wrapper] ******************************************************************************************
ok: [pi] => (item=[u'python-virtualenv', u'virtualenvwrapper'])

TASK [update .bashrc] *********************************************************************************************************
ok: [pi] => (item=export WORKON_HOME=~/.virtualenvs)
ok: [pi] => (item=source /usr/local/bin/virtualenvwrapper.sh)

RUNNING HANDLER [restart lightdm service] *************************************************************************************
changed: [pi]

PLAY RECAP ********************************************************************************************************************
pi                         : ok=6    changed=2    unreachable=0    failed=0

Vagrant lab

Fire up a virtual machine running debian for testing some of these things.

vagrant up
ansible-playbook -l vagrant site.yml 


Self-taught. multilingual programmer working in DevOps automating Linux server administration and deployment with the use of tools like Chef, Ansible, and Docker Swarm.

Read More