Skip to content
Latchkey

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.yml

Common 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

  1. Read the failing task in prepare.yml.
  2. Use the correct package name and manager for the platform image.
  3. Use raw for 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: false

Pick 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:latest

How to prevent it

  • Keep prepare.yml minimal and image-appropriate.
  • Use ansible-ready base images to reduce bootstrap steps.
  • Bootstrap Python with raw before relying on modules.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →