By “Ansible laboratory”, I mean a collection of virtual servers on which you can test your ansible playbooks.
Containers are a somewhat good fit, since you can easily create machines using your base image of choice and discard them as needed without the hassle of managing actual VMs.
My goal here is to create an Ansible lab with Docker containers with the following features:
- Debian base image,
- systemctl available,
- a single Controller with ansible installed,
- an arbitrary number of node containers for Ansible to target,
- ssh enabled between machines and secured by a key.
Get the code here.
Prerequisites and warnings
- This was tested on macos 15 with Apple silicon.
- Podman Desktop (or just podman) installed on your machine.
- YOU NEED TO ADD A KEY PAIR IN THE
lab/Containerfiles/controller/
directory and the public key inlab/Containerfiles/node/
. See more details in the Containerfiles. - If, as in my example, you need sshfs, you need macfuse https://osxfuse.github.io and the
--device /dev/fuse
option when runningpodman run
. - Remember that if your host machine runs on Apple silicon, your arch will be detected as darwin-aarch64 or arm64. This may impact which version of Java you may want to run in your containers for example.
Limitations
Unfortunately, I have not found a way to make firewalls such as nftables or firewalld work from within the containers.
Podman and Docker handle networking in a very different way from a non-containerized VM and, to my knowledge, have no plans to support this since this is very much outside of their intended use case.
Building the images
>lab/$ ./scripts/build-images.sh
Starting the lab
>lab/$ ./start.sh
Then, assuming your Ansible configuration and playbooks are in an ansible/
directory next to lab/
:
Configure your inventory, for example ansible/inventories/lab/hosts.yml
. To reference a node, simply use lab_node1
for example.
Run your playbooks:
# in the Controller
cd ansible
# if you have `requirements.yml`
ansible-galaxy install -r requirements.yml
# run your playbooks
ansible-playbook -i inventories/lab/hosts.yml playbooks/your-playbook.yml