Getting Started with Ansible
Monday, August 28th, 2023
Recent work has involved reviewing some test environments for an IoT development board. The aim is to improve some of the components used for testing as well as adding new functionality. The requirements are:
- Provide an updated version of existing functionality
- Single board environment with all functionality deployed for quick testing
- Cluster distributing the test environment for load testing
The most cost effective way to do this is to use a number of Raspberry Pi single board computers. These boards are now becoming available in quantities after several years of limited availability.
The Problem
How to setup the environment in such a way that will allow a fresh environment to be created reliably.
Enter ansible.
Ping
First step, try to contact a board and this is where ping comes in. This command will verify that ansible can connect to a board. The following command will test the connection to each board:
ansible cluster -m ping -i hosts
This command requires a text file hosts containing the list of boards to the contacted. The file is simple and may only contact two lines:
[cluster] node
In the above example, the file defines a group of machines to be contacted and this is named cluster and in this case the group contains only one machine and this is named node. The name cluster is also mentioned in the ansible command above.
Additional machines can also be named under the cluster entry by simply placing additional entries on a new line in the file.
So far this is nothing new and it is covered in the Ansible documentation.
What Happened
The first step was to use the Raspberry Pi Imager application to create a new image on a new SSD. Nothing complex:
- Raspberry Pi 64-bit Lite OS
- Set the machine name to be node
- Enable SSH
- Set the user name to clusteruser and give the user a secure password
The password was then stored on the local machine in an environment variable CLUSTER_PASSWORD to allow the scripts to be stored in source control without giving away any secrets.
Time to test the connection with the following command:
ansible cluster -m ping -i hosts --extra-vars "ansible_user=clusteruser ansible_password=$CLUSTER_PASSWORD"
Breaking this down, we want to ping all of the machines defined in the cluster group. The group is defined in the file hosts and we are going to log on to the machines with the user name clusteruser and with the password contained in the CLUSTER_PASSWORD environment variable.
Now running the above command results in the following:
node | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
Conclusion
A good start to the project, now on to something more complex, time to install and configure some software.
And I can’t believe I’ve missed Ansible for so long.