How do you test roles that manage systemd services inside Molecule containers?
AdvancedProductionTesting, CI/CD and Quality
Answer
A normal container has no init system, so systemd tasks fail with "System has not been booted with systemd". Use an image that runs systemd as PID 1 and give it the cgroup access it needs.
platforms:
- name: ubuntu2404
image: geerlingguy/docker-ubuntu2404-ansible:latest
pre_build_image: true
command: "" # let the image start /lib/systemd/systemd
cgroupns_mode: host
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
On cgroup v2 hosts cgroupns_mode: host plus the rw mount is what makes it work. Avoid privileged where your CI forbids it; Podman can run systemd containers with systemd: always without full privilege. If a service still behaves differently from a VM, move that role to a VM scenario rather than hacking around it.
More on Testing, CI/CD and Quality
What does a testing pyramid look like for Ansible code?BeginnerHow do you roll out `ansible-lint` profiles across a team without blocking everyone?IntermediateWhen do you use `skip_list`, `warn_list` and `# noqa`?IntermediateHow do you set up pre-commit hooks for an Ansible repo?BeginnerHow is a Molecule scenario laid out, and what does `molecule test` actually run?IntermediateDocker, Podman or real VMs as the Molecule driver: how do you choose?Advanced