The jfxs/ansible image with Molecule and its `docker` driver to run the Molecule tests in CI
193
ansible-moleculeThe jfxs/ansible image with Molecule and its docker driver, to run the Molecule tests of Ansible roles in a CI without installing the Python libraries at each job.
jfxs/ansible image (ansible-core, ansible-lint, testinfra, yamllint, go-task...),molecule, molecule-plugins[docker] and the Docker Python SDK (docker). No Docker CLI: use the community.docker.docker_api connection in molecule.yml, which only needs the SDK and DOCKER_HOST,multiarch with support of amd64 and arm64,Molecule and its Python dependencies),SBOM) attestation,The collections (community.docker, ansible.posix...) are not in the image, as in jfxs/ansible: the dependency step of Molecule installs them. The platform images, with systemd, are jfxs/molecule-ubuntu and jfxs/molecule-debian.
molecule:
image: jfxs/ansible-molecule
services:
- docker:dind
variables:
DOCKER_HOST: "tcp://docker:2375"
DOCKER_TLS_CERTDIR: ""
script:
- molecule test
With the docker driver, the molecule.yml platforms need privileged: true, cgroupns_mode: host and the /sys/fs/cgroup volume (see the platform images). The connection of the instances:
provisioner:
name: ansible
inventory:
host_vars:
instance:
ansible_connection: community.docker.docker_api
# The docker_api connection does not read DOCKER_HOST from the environment.
ansible_docker_docker_host: "{{ lookup('ansible.builtin.env', 'DOCKER_HOST') | default('unix:///var/run/docker.sock', true) }}"
Locally, with the socket of your Docker daemon (-u 0 is only needed to access the socket, the image runs as non-root otherwise):
docker run -it --rm -u 0 -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":/ansible jfxs/ansible-molecule molecule test
task install-templates # fetch the task templates (first time)
task 00:010-build-local # build ansible-molecule
task 00:020-test-local # tests/acceptance.sh
Without task: docker build -t ansible-molecule . then tests/acceptance.sh ansible-molecule.
tests/acceptance.sh checks that molecule, the Docker SDK and the Molecule plugins work, that the image runs as non-root and with an arbitrary UID, and runs a complete Molecule scenario (tests/molecule/default: create, converge, idempotence, verify, destroy) on a jfxs/molecule-debian platform. It needs a Docker daemon that accepts privileged containers and the host cgroup namespace.
Dockerfile: the base image (IMAGE_ANSIBLE, tag of the jfxs/ansible minor version and digest) and ARG VERSION, the Molecule version.requirements.txt: the other Python packages added to the base image (molecule-plugins, docker and their dependencies, pip freeze format).Renovate bumps them all, the Python ones in a single merge request. The build keeps the packages of the base image as they are (pip constraints): if a new Molecule needs another version of one of them, the build fails until the base image is updated.
To regenerate requirements.txt after adding a package, in the base image:
docker run --rm -u 0 --entrypoint sh jfxs/ansible -c 'pip freeze --exclude-editable > /tmp/base.txt && pip install -q -c /tmp/base.txt molecule=="<version>" "molecule-plugins[docker]" && pip freeze | grep -vxFf /tmp/base.txt'
(remove molecule from the result: its version is the ARG VERSION of the Dockerfile).
Docker latest tag is 26.10.0-001, 26.10, 26.
Dockerhub Overview page has the details of the last published image.
Docker tag definition:
molecule version used,<molecule_version>-<increment>
Example: 26.10.0-001
The image is signed with Cosign and carries a SBOM attestation:
cosign verify --key cosign.pub $IMAGE_URI
cosign verify-attestation --key cosign.pub --type spdxjson $IMAGE_URI | jq '.payload | @base64d | fromjson | .predicate'
The public key is available online: https://gitlab.com/op_so/docker/cosign-public-key/-/raw/main/cosign.pub.
This program is free software: you can redistribute it and/or modify it under the terms of the MIT License (MIT). See the LICENSE for details.
docker pull jfxs/ansible-molecule:latest