Skip to content

Installing Ansible

Prerequisites

  • A computer or laptop running a Linux distribution (e.g., Ubuntu, Debian, CentOS) or macOS. Ansible can also be used on Windows, but it's simpler to install it on a Unix-like system.
  • Administrative or superuser access to your local machine to install software.
  • A reliable internet connection.

Update System Packages (Linux Only)

If you're using a Linux distribution, it's a good practice to update your system packages to ensure you have the latest software.

sudo apt update
sudo apt upgrade

Install Ansible

sudo apt install ansible

Verify Ansible Installation

After the installation is complete, you can verify that Ansible is properly installed by running the following command:

ansible --version

This should display the Ansible version and other information.

Create an Ansible Inventory

You'll also need to create an Ansible inventory file, which lists the Raspberry Pi devices you want to manage. Create a file named hosts in your Ansible directory, e.g., etc/ansible/hosts, and add the IP addresses of your Raspberry Pi devices like this:

vim /etc/ansible/hosts

I added a control group and a workers group. For convenience there is also a cluster group for the combination of both.

[control]
cluster-master  ansible_connection=local

[workers]
cluster-1  ansible_connection=ssh
cluster-2  ansible_connection=ssh
cluster-3  ansible_connection=ssh
cluster-4  ansible_connection=ssh
cluster-5  ansible_connection=ssh
cluster-6  ansible_connection=ssh
<IP-ADDRESS>  ansible_connection=ssh

[cluster:children]
control
workers

Replace <IP-ADDRESS> with the actual IP addresses of your Raspberry Pi devices. I add my Pi's ip adresses to the hosts file of the master node.

Add ip addresses to the master hosts file

Add the new worker ip-address and desired hostname to the list

vim /etc/hosts

127.0.0.1 localhost
127.0.1.1 cluster-master
#::1 localhost ip6-localhost ip6-loopback
#ff02::1 ip6-allnodes
#ff02::2 ip6-allrouters

192.168.86.100 cluster-master

192.168.86.101 cluster-1
192.168.86.102 cluster-2
192.168.86.103 cluster-3
192.168.86.104 cluster-4
192.168.86.105 cluster-5
192.168.86.106 cluster-6

SSH key for ansible

To make sure that we do not have to login to every node when using ansible, i setup ssh keys from the master node to all nodes. This since the master node is the node where ansible commands should be run from.

On the Master Node:

  1. Generate an SSH key pair for the root user (if not already generated). You can do this by running the following command on the master node:
ssh-keygen

Follow the prompts to generate the SSH key. By default, this will create the key pair in the /root/.ssh directory.

  1. Copy the public key to the authorized_keys file on the remote Raspberry Pi nodes. You can do this using the ssh-copy-id command or by manually appending the public key to the authorized_keys file on each node.

Using ssh-copy-id:

ssh-copy-id root@<worker-node-ip>

Repeat this command for each worker node, replacing <worker-node-ip> with the IP address of each node.

Now, you should be able to SSH into the worker nodes as the root user from the master node without being prompted for a password. This will allow you to use Ansible for automated management and configuration of the Raspberry Pi nodes.

SSH risk

Please be aware that enabling root SSH access can pose security risks, and it's recommended to use SSH key authentication and minimize direct root logins whenever possible.

Test Ansible

You can now test your Ansible setup by running a simple ping command to ensure that Ansible can reach your Raspberry Pi devices:

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

If everything is set up correctly, you should see a "pong" response from each Raspberry Pi.

root@cluster-master:/# ansible -i /etc/ansible/hosts workers -m ping
cluster-1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
cluster-3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
cluster-2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
cluster-4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

You have successfully installed Ansible and are ready to automate the configuration and management of your Raspberry Pi devices. This will be invaluable as you proceed to set up your Kubernetes cluster using K3s.