Floating Octothorpe

Raspberry Pi playbooks

I started playing with Ansible a couple of years ago. Recently I've finally got around to porting most of my Raspberry Pi configuration to Ansible. This post is going to look at using those Raspberry Pi playbooks.

Installing Ansible

Ansible needs to be installed onto a control host, this can either be the Raspberry Pi itself, or a remote host with network access. On Raspbian Ansible can be installed with apt-get:

sudo apt-get update
sudo apt-get install ansible

Unfortunately at the time of writing Raspbian ships with Ansible 2.2.1.0. This version of Ansible is missing a feature that was introduced in 2.3 which my playbooks depend on. Thankfully a later version of Ansible can be installed on CentOS via EPEL:

yum install ansible

For more installation info, either have a look at my post on getting started with Ansible, or refer to the Ansible docs which cover installation on other platforms.

Configuring Ansible

Once Ansible is installed the next step is to configure an inventory file. If you're running Ansible locally a line similar to the following should work:

somehost ansible_user=pi ansible_become=true ansible_host=localhost ansible_connection=local

Alternatively if you're planning to run Ansible over SSH, config similar to the following should work:

somehost ansible_user=pi ansible_become=true ansible_host=192.168.56.103 ansible_ssh_pass=raspberry

You can also set up SSH keys to avoid having to hard code your password:

somehost ansible_user=pi ansible_become=true ansible_host=192.168.56.103

Once everything is set up, you should be able to use the ping module to test Ansible:

$ ansible somehost -i inventory -m ping
ein | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Running playbooks

Once Ansible is working, clone a copy of the playbooks with git:

git clone https://github.com/FloatingOctothorpe/pi-playbooks.git

The playbooks are split into separate Ansible roles. The enabled_roles variable is used to control which roles should be applied. This can be set by creating a host specific variables file (e.g. pi-playbooks/host_vars/somehost.yml):

---
enabled_roles:
  - 'apache'
  - 'hostname'
  - 'vim'

hostname: 'somehost.example.com'

In the example above, three roles will be applied. As well as specifying the roles, other variables can be set; for example hostname is used to specify what the system's hostname should be set to. Info on available variables can be found in the tasks/main.yml file for each role.

Once you've created a file in host_vars/ you should be able to run the site.yml playbook. Initially this can be run with the --check option to preview the changes that Ansible will make:

ansible-playbook -i inventory pi-playbooks/site.yml  -v --diff --check

Assuming the changes look OK, run Ansible again without the --check option to apply the changes:

ansible-playbook -i inventory pi-playbooks/site.yml  -v --diff

It's also possible to selectively run a role by explicitly overriding the enabled_roles variable:

ansible-playbook -i inventory pi-playbooks/site.yml  -v --diff \
  --extra-vars="enabled_roles=['vim']"

Contributing

The playbooks are still a work in progress, and will likely be updated over time. Please feel free to fork the repo, or raise pull requests if you have suggested changes.