Installing Ansible 2.2.0 on a Raspberry Pi

Ansible is a powerful IT automation tool with lots of modules and active development. Unfortunately, the only version available for stock Raspberry Pi is old. I wanted to use some of the newer modules and features, so I needed to install at least version 2.2. This tutorial will walk you through installing Ansible 2.2.0 on a Raspberry Pi running Raspbian “jessie”. The version of Raspbian I did this on was tagged “2016-09-23”.

Creating a Debian package for Ansible requires a lot of prerequisites, including TeX Live, which can take up almost 2GB of space. If you don’t want to do this yourself, you can try downloading the Ansible package I built. It might be old by the time you read this, though!

Install Prerequisite Packages

Log into the terminal of your Raspberry Pi and run the following command to install the required packages.

sudo apt install asciidoc devscripts python-dev libffi-dev libssl-dev cdbs sshpass -y

Download and Prepare the Ansible Repo

Ansible is available for download from GitHub. Use the following commands to retrieve the Ansible source code and modules.

git clone git://github.com/ansible/ansible.git --recursive
cd ansible/

Use these commands as a guide to select and checkout a stable Ansible release. The list of tags should give you clues as to which tag is the latest and not a development version.

git tag -l
git checkout v2.2.0.0-1
make deb

Your new .deb package will be located in ./deb-build/unstable/. You can install it with the dpkg command like this.

sudo dpkg -i ./deb-build/unstable/ansible_2.2.0.0-100.git201611010320.cdec853.HEAD~unstable_all.deb

Using the Newest Ansible Features

My goal was to use Ansible on my Raspberry Pi to implement my poor-man’s dynamic DNS solution. However, you might also want to use your RPi as a centralized Ansible hub to control a cluster like the Raspberry Pi Dramble. Also consider checking out the ansible-simple-slurm-cluster repo on GitHub for more ideas.

Let me know how you’re using Ansible on your Raspberry Pi in the comments!

A Poor-Man’s Dynamic DNS with Ansible and Amazon Route53

Traveling Route53

I wanted to be able to configure a DNS hostname dynamically, but couldn’t find an easy-to-use dynamic DNS client that suited my needs. Using Ansible and Amazon Route53, I put together a quick, effective solution.

Requirements

First, you need an AWS account with a Route53 DNS zone. I followed these directions to create a subdomain.

Next, you need a remote host that accessible via SSH. On that host, install Python and the Boto library. Make sure that Boto is configured with sufficient AWS credentials to access and change your Route53 zone.

Ansible Configuration

This section was updated on 2016-11-29 to reflect improvements I’ve made in the Ansible playbook.

Ansible made this task simple. In fact, the playbook below is mostly based on example recipes from the Ansible Route53 module documentation. The YAML playbook should look like the example below. Replace YOUR-ROUTE53-ZONE with the zone you configured in Route53. Replace YOUR-FULL-DYNAMIC-HOSTNAME with the fully-qualified domain name that you’ll use for dynamic DNS.

Note that this uses the ipify_facts Ansible module. You can use the default value or pass api_url like I’m doing in this example.

---
- name: Update Dynamic IP
  hosts: localhost
  vars:
    dyn_zone: YOUR-ROUTE53-ZONE
    dyn_hostname: YOUR-FULL-DYNAMIC-HOSTNAME
  tasks:
    - name: Get public IP
      ipify_facts: api_url=https://arnesonium.com/api/yourip.php
      connection: local
    - name: Get existing host information
      register: dynip
      route53:
        command: get
        zone: "{{ dyn_zone }}"
        record: "{{ dyn_hostname }}"
        type: A
    - name: Delete existing host information
      when: ipify_public_ip != dynip.set.value
      route53:
        command: delete
        zone: "{{ dyn_zone }}"
        record: "{{ dynip.set.record }}"
        ttl: "{{ dynip.set.ttl }}"
        type: "{{ dynip.set.type }}"
        value: "{{ dynip.set.value }}"
    - name: Create new host record
      when: ipify_public_ip != dynip.set.value
      route53:
        command: create
        zone: "{{ dyn_zone }}"
        record: "{{ dyn_hostname }}"
        type: A
        ttl: 600
        value: "{{ ipify_public_ip }}"

Running Your Playbook

I named my playbook dyndns.yml, so I run it with this shell command:

ansible-playbook -vv dyndns.yml

The -vv increases the verbosity so you can see what’s going on.

The Next Step

Next, I need to convince this script to run every time my laptop’s network comes back online. I’m sure there’s a good way to do that, but I haven’t spent much time looking into it.

Did this playbook work for you? Let me know! I’d love to get feedback on it.