Scripting personal machine setup with ansible

February 2, 2014

This all started becasue of two things…

  • I’ve been working alot with Ansible, I’ve fallen in love with this tool, it’s made a lot of my ops automation tasks so much easier.
  • I managed to kill my laptop, so I was about to be setting up everything for the second time this year and I really didn’t want to do it again.

Getting started

First thing’s first, I installed Debian Testing/Jessie selecting only the “Base” and “Laptop” packages. If you’re cool with the default desktop environment you can select “Desktop”, but I wanted something a bit more custom.

Prepare the machine

With a clean install you’ll need to get Ansible and it’s dependencies installed. So I created a script that can be run as a one off to bootstrap the machine.

#!/bin/bash
apt-get install -y python-pip build-essential python-dev sudo
pip install ansible 
echo 'chris ALL=(ALL:ALL) :ALL' > /etc/sudoers.d/chris 

We need pip to install Ansible, build-essential/python-dev for native Ansible dependencies and finally sudo to allow Ansible to act as root.

Creating first playbook and role

We’ve now got a fairly bare machine, I need an editor so I can easily edit ansible yml files. So I started with the following structure.

/roles/text_editor/tasks/main.yml
/playbook.yml
/hosts

Even through we’re running locally Ansible requires a hosts file, so to satisfy it I have a file that just refers to the local host.

[localhost]
127.0.0.1

By default Ansible will try and use a SSH connection to the host to perform the operations. Since we’re running locally and we don’t even have SSH installed yet our playbook need to change the connection type to local and force it to only work on the local host.

---
- hosts: 127.0.0.1
  connection: local
  roles: 
   - text_editor

Finally, my text editor role had this tasks file…

---
- name: install vim
  apt: pkg=vim-nox state=present

Now the whole thing could be run with

ansible-playbook -i hosts -vvvv playbook.yml

What next

  • There are always a few tweaks that are needed to help with power management, proprietory hardware etc that can be automated.

  • Create separate roles for user level changes so that they can be run locally or on any machine you wish to have setup the way you want it. Things like…

    • .vimrc (and plugins)
    • .bashrc
    • .gtkrc-2.0
    • settings.ini (gtk-3.0 settings)
  • Push the whole thing to a git repo somewhere else so you have a backup.

  • What ever you don’t keep anything sensitive in here like passwords if you’re pushing to GitHub. And for the extra paranoid you might want to keep it a private repo so you’re not advertising what you have running on your machine and on what ports etc.

Oh yeah, and i also…

Combining Ansible and Vagrant ended up being an excellent way to setup and maintain working environments for a team. Providing a way to share changes to environment setup in an automated way. It also helped with new people joining the team, they could get up to speed a lot faster.