Molecule "prepare" step failed in CI
Molecule runs prepare.yml after create to bootstrap the instance (install Python, seed users, add repos) before converge. A failing task in prepare stops the sequence before your role ever runs.
What this error means
Molecule reports "Ansible return code was 2, command was: ... prepare" and the failing task from prepare.yml in the log.
Molecule
TASK [Install python] ***
fatal: [instance]: FAILED! => {"msg": "No package matching 'python3' found"}
CRITICAL Ansible return code was 2, command was: ansible-playbook ... prepare.ymlCommon causes
A bootstrap task fails on the base image
prepare.yml assumes a package manager or package name that the platform image does not have, so bootstrapping fails.
The instance is not ready when prepare runs
A minimal image without Python breaks Ansible modules until prepare installs it; if prepare itself needs Python, it deadlocks.
How to fix it
Fix the bootstrap for the target image
- Read the failing task in
prepare.yml. - Use the correct package name and manager for the platform image.
- Use
rawfor the very first Python bootstrap when the module system is not yet usable.
molecule/default/prepare.yml
# molecule/default/prepare.yml
- name: Bootstrap python (raw, before modules work)
hosts: all
gather_facts: false
tasks:
- name: Ensure python3 is present
raw: (apt-get update && apt-get install -y python3) || (dnf install -y python3)
changed_when: falsePick an ansible-ready base image
Images preloaded with Python avoid most prepare failures.
molecule/default/molecule.yml
platforms:
- name: instance
image: geerlingguy/docker-ubuntu2204-ansible:latestHow to prevent it
- Keep prepare.yml minimal and image-appropriate.
- Use ansible-ready base images to reduce bootstrap steps.
- Bootstrap Python with
rawbefore relying on modules.
Related guides
Molecule "converge" playbook failed in CIFix a failing Molecule converge in CI - converge.yml applies your role to the instance, and any failed task r…
Molecule "Failed to create instance(s)" (docker) in CIFix Molecule "Failed to create instance(s)" in CI - the create step could not start a container because the d…
Molecule "Failed to connect to the host via ssh" in CIFix Molecule "Failed to connect to the host via ssh" in CI - the scenario uses an ssh connection to an instan…