Provision a Raspberry Pi (or any PC) as a router on a stick and enable communication between home and lab network with NAT.
I needed a lot of routers, since I had some spare Raspberry Pis, that's what I used.
I created this simple Ansible Playbook to easily convert my RPis without having to type the same commands a thousand times.
- Prepare the Raspberry Pi from scratch (upgrade, dist-upgrade, network configuration)
- Using a single NIC for both networks (LAN and LAB) to work as a router on a stick
- Defining a VLAN interface
- Defining static IPs for these networks
- NAT enabled from LAB to LAN network
- (hopefully) Optimized Ansible Playbook to install everything with as few tasks as possible
- Easy to use with a single command:
just
- One RPi at a time because I'm using
group_vars
variables for now and all the hosts would have the same IP - can be fixed if I define, at least, the LAN et LAB IPs insidehost_vars
and create a file for each RPi - this has not been tested yet but it would surely work!
Here is an example of infrastructure using a RPi as a router on a stick to bridge home network with lab network :
- A PC/RPi (router) with network access and a single NIC (tested with a Raspberry Pi 4 - 4 GB of RAM)
- Any Debian based distribution (tested with Raspberry Pi OS Lite (64-bit) - Bullseye)
- A PC (client) to execute the playbook from and apply on the RPis (tested with a RPi 4 and Raspberry Pi OS Lite 64-bit - Bullseye)
- 2 networks with hosts in each to test connectivity
- Python 3 (tested with 3.9.2)
- pip (tested with 20.3.4)
- Ansible (tested with 2.14.5)
- Just (optional)
- Git (duh)
As a side note, all the Micro SD cards of the RPis were prepared using Raspberry Pi Imager
Clone the repository:
git clone https://github.com/AngeIo/rpi-router-ansible.git
cd rpi-router-ansible
To update the source code to the latest commit, run the following command inside the rpi-router-ansible
directory:
git pull
Here are all the steps you have to follow to make this work:
The first thing you have to do is editing the variables to match with your environment (comments in the files shows what to edit) :
vi ansible.cfg
vi inventory/all
vi group_vars/raspberrypi
Running the playbook is very simple:
just
OR
ansible-playbook -k -D pb_main.yml
Your newly created RPi router should be able to join both networks and, if the gateway (IP in LAB of RPi router) is properly setup on any host inside the lab, they should also communicate with the Internet and LAN/home network!
If you want to contribute to this project, feel free to submit a pull request, I'll be happy to merge it! Everyone is welcome!
This project's code is licensed under The Unlicense. Please see the license file for more information. tl;dr you can do whatever you want with it, it's public domain.