Automating Raspberry Pi K3s provisioning with Ansible
Tags: ansible, iot, raspberrypi
Published: 2021-12-15 03:50:06

---
title: Automating Raspberry Pi K3s provisioning with Ansible
published: true
date: 2021-12-15 03:50:00 UTC
tags: ansible, iot, raspberrypi
canonical_url: https://sahansera.dev/k3s-rapsberrypi-ansible-automation/
---

In this article, we will be looking at how we can automate most of the steps involved in installing a Raspberry Pi+K3s cluster by using Ansible.

I explained how to set up a Kubernetes cluster on a Raspberry Pi in my previous article. In case you missed it, you can find it from the following link:

🔗 **[Building your own private Kubernetes cluster on a Raspberry PI 4 with K3S](https://sahansera.dev/building-your-own-private-kubernetes-cluster-on-a-raspberry-pi-4-with-k3s/)**

If you have a fresh bunch of Raspberry Pis, you might want to follow Step 0 through to Step 8 in the above. However, it’s a one-off setup, so please proceed to the following sections if you already have done them.

In a nutshell, this is what we are trying to automate.

![k3s-rapsberrypi-ansible-automation-0.png](https://sahansera.dev/static/d39f0323d272251c6acceddf6c3761df/5a190/k3s-rapsberrypi-ansible-automation-0.png "k3s-rapsberrypi-ansible-automation-0.png")

### Step 1 - Installing Ansible

So what do we need to get started? You need one or more RPis with Raspbian installed (or an OS [supported by K3s](https://rancher.com/docs/k3s/latest/en/installation/installation-requirements/#operating-systems)). We can get to our desired state by using an Ansible playbook.

[Ansible](https://www.ansible.com/) is an industry-leading IT automation tool to provision and manage your infrastructure in a declarative form such YAML.

The easiest way to get the steps to installing Ansible would be to head over to the [official docs](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html). For most OSs, it can be just installed via `pip`.

```bash
python -m pip install --user ansible
```

> 💡 Note: Use the latest version of Python installed on your machine. In my case, I have it set to `python3` since Mac OS comes with an older version of Python installed.

Depending on your system and the internet connection, it will a bit of take time to install Ansible. Sometimes even if you installed it, it would still say that it can’t find `ansible` command. If that’s the case, feel free to follow this [link](https://stackoverflow.com/questions/63177609/zsh-command-not-found-ansible-after-pip-installing). If you are on a Mac, you could also run a `brew install ansible` to install ansible.

![k3s-rapsberrypi-ansible-automation-1.png](https://sahansera.dev/static/70581b905961f6da018dc7d0db07d5c3/5a190/k3s-rapsberrypi-ansible-automation-1.png "k3s-rapsberrypi-ansible-automation-1.png")

### Step 2 - K3s Ansible Playbook

Without writing everything from the ground up, we will be using the [k3s-ansible](https://github.com/k3s-io/k3s-ansible) repo.

```bash
git clone git@github.com:k3s-io/k3s-ansible.git
```

Once you have cloned the repo, you can run the following command to create a new playbook for your RPi cluster provisioning. Make sure to replace `my-cluster` with the desired name for your cluster. In my case, I called it `rpi-cluster`

```bash
cp -R inventory/sample inventory/my-cluster
```

Next, we need to map out the nodes and master them in the `inventory/my-cluster/hosts.ini` file. Update the `[master]` and `[node]` attributes accordingly.

```txt
[master]
10.0.0.100

[node]
10.0.0.101

[k3s_cluster:children]
master
node
```

Under `[node]` attribute you could even specify a range of IPs if they are consecutive like so.

```txt
[node]
10.0.0.[101:105]
```

Check the `inventory/my-cluster/group_vars/all.yml` to set variables such as `k3s_version` and `ansible_user`, depending on your environment.

### Step 3 - Running the Ansible Playbook

```bash
ansible-playbook site.yml -i inventory/rpi-cluster/hosts.ini --ask-pass
```

![k3s-rapsberrypi-ansible-automation-2.png](https://sahansera.dev/static/bb872c572dedb1a2894597c50e238a37/5a190/k3s-rapsberrypi-ansible-automation-2.png "k3s-rapsberrypi-ansible-automation-2.png")

You can now SSH into the Pi master or nodes and run `kubectl` commands within it.

if you want to uninstall everything that got installed as part of the playbook:

```bash
ansible-playbook reset.yml -i inventory/rpi-cluster/hosts.ini --ask-pass
```

### Conclusion

This article looked at how we can automate provisioning our Raspberry Pi with Ansible. If you are new to DevOps and want to automate provisioning systems, this is a great start. A special shoutout to [@itwars](https://github.com/itwars) for putting this together and doing a lot of heavy lifting creating these scripts.

### References

- [https://github.com/k3s-io/k3s-ansible](https://github.com/k3s-io/k3s-ansible)