Interview Prep/Ansible/Testing, CI/CD and Quality

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.

All Ansible questions Roadmap: Ansible

More on Testing, CI/CD and Quality