Skip to main content

Testing Ansible playbooks in a Coder workspace

2 min read
#coder #ansible #docker #terraform #linux

Overview

This project builds a self-hosted Coder server with one workspace, ansible-sandbox, for testing Ansible playbooks. The workspace has the same Ansible versions as my work jumpbox, and it starts three test servers of its own: Rocky Linux 8, Rocky Linux 9 and Ubuntu 22.04. A playbook can run against all three from the browser or from VS Code, without touching a real server.

My main tool for testing Ansible roles is Ansible Molecule. This project is only a proof of concept: it shows that the same kind of testing can be done inside a Coder workspace.

This is only for my homelab. It is not a production setup.

Tools used

  • Coder: runs the workspace and gives browser, SSH and VS Code access to it
  • Terraform: the Coder template that describes the workspace is written in Terraform
  • Docker: runs Coder itself, and a second Docker engine inside the workspace runs the test servers (Docker-in-Docker)
  • Dev containers: each test server is a dev container with systemd, so services start like on a real server
  • Ansible: the version under test, installed with uv in a Python 3.11 venv, plus ansible-lint, Molecule and yamllint
  • authentik (optional): single sign-on with MFA for Coder; Coder’s own login works without it
  • Proxmox: hosts the Coder VM

How it works

  1. A Proxmox VM runs Coder, its Postgres database and Traefik, installed by an Ansible role
  2. A Coder template (infra-lab) describes the workspace in Terraform, and is kept in a Git repo
  3. When the workspace starts, it starts its own Docker engine, installs the pinned Ansible and Terraform versions, and clones the Git repo
  4. Coder starts the three test containers from their devcontainer.json files
  5. Ansible connects to the containers with docker exec, so no SSH setup is needed, and the inventory only lists the test containers, so a test can never reach a real server

What I learned

  • The Coder host must be a VM, not an LXC container. An unprivileged LXC blocks what Docker-in-Docker and systemd need.
  • Docker inside Docker needs its own volume for /var/lib/docker and a cgroup setup step before dockerd starts.
  • Dev containers replace the start command with a sleep loop by default. "overrideCommand": false keeps systemd running.
  • Rocky 8 has Python 3.6, which is too old for the newest ansible-core. Pinning the same versions as work (ansible-core 2.16.2) fixed it.
  • 1 GB of memory froze the VM. 4 GB, with a memory limit on the workspace, works.

Step-by-step guide

All the steps and files are in my note: Build a Coder lab for testing Ansible playbooks.