If you’re like me and you have a home lab you are constantly changing things, whether that is changing the way your network works or adding new hardware. For me that meant every time I got a new server I had to manually configure things. Some of it was easier than others, however a constant pain has been moving my docker containers from host to host without using any kind of orchestration tools like Kubernetes.
Enter Ansible. Anisble is a configuration management tool that helps lots of Sysadmin and engineers to manage fleets of servers reliably. I use Ansible everyday in my day job to deploy all sorts of things to the very large amount of servers that I’m responsible for, but at home I still “handjam” everything. But after months ( maybe years) of resisting – who wants to come home to do more work – I’ve finally decided to manage the servers at home the way I manage them at work.
The first project I decided to tackle was to automate some simple tasks, the first one being installing docker. So I created an extremely simple play that just that.
With just those few lines of YAML, I’m able to install the pre-reqs, set up the docker repository, install docker, and add myself to the docker group.
Next I decided to tackle mounting my nfs mounts that my Plex server uses to serve my movies.
This creates the directories that my nfs shares backed by Freenas would be mounted to. One of the import mount points is /config. This is the directory that does the magic for my docker containers that save state. As long as I mount this share and bind the correct path to the containers my container configs/databases will always be the same regardless of how many times I rebuild my docker hosts
With all of that working I can now use Ansible to spin up my containers
And that’s it, I can keep writing plays similar to the one above and my containers will be created and started automatically with all the correct options. And because Ansible is idempotent I never have to worry about stepping on any running containers, I simply modify my play, run Ansible and off to the races.
One thing to note is that container listed above is excellent . It allows you to declare a wild card DNS A record that points to the container host. Any container with the following env variables, VIRTUAL_HOST and VIRTUAL_PORT are automatically proxied behind the scenes to the mapped URL. For example 192.168.29:9000 can become portainer.foo.bar without having to add more DNS records and manually setting up a reverse proxy.