1. 2016-09-24 - Use Ansible for cluster management; Tags: Use Ansible for cluster management

    Use Ansible for cluster management

    Having a Rasbperry Pi Cluster allows you to learn, test and experiment concepts about distributed computing. The first challenge is always how to keep the pi’s up to date. Having four nodes seems still manageable, but what about 64 nodes? Although it is a little bit repetitive, to enter the exact same commands over and over again. Another friend would see just write a bash script that executes the exact command on each node via SSH. Hmm, wait, there is Ansible that does exactly that for you. So a quick jump into Ansible under Ubuntu.

    Ansible Installation

    The installation under Ubuntu is pretty simple.

    $ sudo apt-get install software-properties-common
    $ sudo apt-add-repository ppa:ansible/ansible
    $ sudo apt-get update
    $ sudo apt-get install ansible

    Check the official installation for any other systems.

    Prepare Cluster

    Ensure that your pi cluster is running and network is properly setup. You can use nmap to detect which IP’s were assigned. BTW, my cluster uses solely WLAN.

    tan@omega:/etc/ansible$ nmap -snP
    Starting Nmap 7.01 ( https://nmap.org ) at 2016-09-24 19:45 CEST
    Nmap scan report for omega.home (
    Host is up (0.00041s latency).
    Nmap scan report for beta.home (
    Host is up (0.012s latency).
    Nmap scan report for alpha.home (
    Host is up (0.012s latency).
    Nmap scan report for gamma.home (
    Host is up (0.011s latency).
    Nmap done: 31 IP addresses (4 hosts up) scanned in 1.92 seconds

    Configure hostnames

    Having the IP addresses is our first step. Ansible doesn’t need semantical hostnames, but I recommend to do so. With another WLAN router they will have other IP addresses. In /etc/hosts we can assign the semantical hostnames.

    tan@omega:/etc$ cat /etc/hosts	localhost	omega	alpha	beta	gamma	delta
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters

    Setup SSH

    Ansible needs SSH. Ensure that the ansible host can login with SSH to the machines. I just use key authentication. Test the ssh connection. The first time you connect the fingerprint will be added to your known_hosts file.

    tan@omega:~$ ssh root@delta
    The authenticity of host 'delta (' can't be established.
    ECDSA key fingerprint is SHA256:a6OMvgyfMUhcbq2RNM2KuvjbNMpC2p233/FmdAeF50Y.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'delta' (ECDSA) to the list of known hosts.
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Sun Sep 18 18:38:51 2016 from omega.home
    root@delta:~# logout
    Connection to delta closed.

    Setup Ansible

    If Ansible is installed, the Inventory is defined in /etc/ansible/hosts. I just add the hostnames to that file.

    # This is the default ansible 'hosts' file.
    # It should live in /etc/ansible/hosts

    Test Ansible SSH access

    Test the connection

    tan@omega:~$ ansible all -m ping -u root
    alpha | SUCCESS => {
        "changed": false,
        "ping": "pong"
    gamma | SUCCESS => {
        "changed": false,
        "ping": "pong"
    beta | SUCCESS => {
        "changed": false,
        "ping": "pong"
    delta | SUCCESS => {
        "changed": false,
        "ping": "pong"

    Update your cluster

    Now you can update your cluster in parallel with

    $ ansible all -u root -m shell -a 'apt-get update'
    $ ansible all -u root -m shell -a 'apt-get upgrade -y'

    Knowing now the mechanics, you could just use a playbook.yml to automate it. See todo add link.