Raspberry Pi and Ansible Part 2

Installing Ansible

Now, we need to install Ansible on our server. My primary host machine is a Mac Mini. So, to install on my Mac you will likely need XCode installed.

To install Ansible, use Pthyon:

  • sudo easy_install pip
  • sudo pip install ansible —quiet

Later, if you want to upgrade Ansible, simply run:

  • sudo pip install ansible —upgrade

Configure Ansible

Finally, you need an inventory file. It’s similar to a hosts file, but can contain multiple options to handle connecting to the server. These can include login name, password, ports, etc.

 

A hosts file might look like:
[pi]
rpi.local ansible_user=”pi” ansible_ssh_pass=”password”
192.168.1.112 ansible_user=”pi” ansible_ssh_pass=“password”
192.168.1.71 ansible_user=pi ansible_ssh_pass=“password”

[onion]
omega-90a2.local ansible_user=pi ansible_ssh_pass=”onioneer”
omega-8082.local ansible_user=pi ansible_ssh_pass=”onioneer”

This is a text file, so any editor will work (nano, vi, emacs, etc.)

The [xx] represents a grouping of devices. By grouping, you can act on all hosts, the groups or individual hosts. But, most importantly, the other options allow you to specify connection options.
For the record, it is not a best practice to include the password in the hosts file, but it is available for testing. The best method is to use public keys.

First Operation

The first operation you should before is a ping. This will ensure everything is wokring as expected.

I placed my hosts file in ~/ansible. So, to execute the ping against all the hosts, the following command is used:

ansible all -m ping -i ~/ansible/hosts

 

 

Ansible is the name of the application. All says to run the command against all the systems. -m ping is the “run command” and “ping” is the command. -i ~/ansible/hosts contains the inventory files. In my case, four systems, two Raspberry Pis and two Onion Omega2+ systems.

If all goes well, you should get a message simiar to this:
rpi.local | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

You might get a message regarding authenticity. If you do, type yes and press enter. If you get a failure, it could be due to the system being unavailable, such as this:
omega-90a1.local | UNREACHABLE! => {
“changed”: false,
“msg”: “[Errno 8] nodename nor servname provided, or not known”,
“unreachable”: true
}

In this case, the system was offline so the ping couldn’t connect. Next lesson, let’s do something useful!

Raspberry Pi and Ansible Part 1

I have been looking for a way to manage my growing collection of Raspberry Pis. I have a collection, including a Pi 2, Pi 3 and a handful of Zeros.

The challenge with managing a collection of devices is making sure they are all up-to-date. Sure, I could spend the time to log into each one, setting up the wifi-information, updating the OS, installing the common packages. I could create a disk image and copy it from SD card to a new install, but what about maintaining packages? What if I want to add an application to one or more of them? Or I could simply manage ssh into each and every one of them and do as I need. But, the question is… Do I want to manage these devices or use them?

 

If you aren’t familar, Ansible is an open source package for managing your machines. It is agentless. It connects to systems using services typically installed by default (ssh). Using this automation technique, you can use the system to access software on your machine(s).

Installing software on the RPi typically involves accessing the package manager APT-GET. If you aren’t familar with it, you may simply being using the UI version. But, using the command line version is fast and efficient, if you know what you want to install.

Apt-get must be kept up to date. So before any installation, an administrator or user issues the following command:

sudo apt-get update

This will update the repositories. Then after that, you typically install software by running a command like:

sudo apt-get install motion

This would install the motion package on the Raspberry Pi. In order to not sit around and wait for the machine to finish the update process, some users might combine the two actions:

sudo apt-get update && sudo apt-get install motion

This works great if you want to install the package on one machine, but what if you want to install it on 5? That means going to 5 machines (manually or using SSH) and running those commands. Then you hae to verify the output of each of those machines to ensure they installed correctly.

Or what if you wanted to set up a series of applications on a set of machines? This quickly becomes a problem. Enter Ansible. Using a hosts file, called an inventory file, you can automate the actions into Playbooks.

These playbooks can be set up to execute the actions on any host in the inventory file or against certain hosts. For example, if you have Redhat and Raspberry Pis, the package managers are different. Yum versus apt-get. You can selectively execute different commands on each machine using the same inventory file.