The Problem

Your Ansible role's Molecule tests fail:

bash
ERROR: Container creation failed: docker: Error response from daemon: conflict

Or:

bash
ERROR: Scenario 'default' failed: ansible-playbook returned non-zero exit code

Or testinfra verification fails:

bash
FAILED test_myrole.py::test_config_file - AssertionError: File /etc/myapp.conf does not exist

Why This Happens

Molecule test failures stem from:

Docker/container issues - Container driver can't create or connect to containers.

Playbook errors in test - Converge playbook has syntax or runtime errors.

Testinfra test failures - Verification tests don't match actual state.

Scenario configuration - molecule.yml has wrong driver or platform settings.

Dependency issues - Role dependencies not installed for tests.

Diagnosing the Issue

Run Molecule with verbose output:

bash
molecule test -vvv

Check specific stages:

bash
molecule create -vv    # Container creation
molecule converge -vv  # Playbook execution
molecule verify -vv    # Testinfra tests

Check container status:

bash
molecule list
docker ps -a

Inspect molecule.yml:

bash
cat molecule/default/molecule.yml

The Fix

Fix 1: Fix Docker Driver Issues

Ensure Docker is running:

bash
docker info
docker ps

Pull required images:

bash
docker pull geerlingguy/docker-ubuntu2004-ansible:latest
docker pull geerlingguy/docker-centos8-ansible:latest

Configure molecule.yml properly:

```yaml # molecule/default/molecule.yml driver: name: docker platforms: - name: ubuntu-instance image: geerlingguy/docker-ubuntu2004-ansible:latest pre_build_image: true privileged: true volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro

  • name: centos-instance
  • image: geerlingguy/docker-centos8-ansible:latest
  • pre_build_image: true
  • privileged: true
  • volumes:
  • - /sys/fs/cgroup:/sys/fs/cgroup:ro
  • `

Clean up containers:

bash
molecule destroy
docker container prune

Fix 2: Fix Converge Playbook Errors

Check converge playbook:

yaml
# molecule/default/converge.yml
- name: Converge
  hosts: all
  become: true
  vars:
    # Define required variables
    myapp_port: 8080
  roles:
    - role: myrole
      # Pass required variables

Test converge separately:

bash
molecule create
molecule converge -vv

Check syntax:

bash
ansible-playbook molecule/default/converge.yml --syntax-check

Fix 3: Fix Testinfra Verification Tests

Check test file:

```python # molecule/default/tests/test_myrole.py import os import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE'] ).get_hosts('all')

def test_package_installed(host): pkg = host.package('nginx') assert pkg.is_installed

def test_service_running(host): svc = host.service('nginx') assert svc.is_running assert svc.is_enabled

def test_config_file(host): f = host.file('/etc/nginx/nginx.conf') assert f.exists assert f.is_file assert f.contains('worker_processes') ```

Run tests separately:

bash
molecule verify -vv

Use correct assertions:

```python # Common assertions def test_file_exists(host): f = host.file('/path/to/file') assert f.exists assert f.is_file assert f.mode == 0o644 # Note: octal

def test_service(host): s = host.service('servicename') assert s.is_running assert s.is_enabled

def test_package(host): p = host.package('pkgname') assert p.is_installed assert p.version.startswith('1.')

def test_command(host): cmd = host.run('mycommand') assert cmd.rc == 0 assert 'expected output' in cmd.stdout

def test_user(host): u = host.user('myuser') assert u.exists assert u.group == 'mygroup' assert u.home == '/home/myuser' ```

Fix 4: Configure Multiple Scenarios

Create scenario-specific configurations:

bash
molecule init scenario centos
molecule init scenario ubuntu

Directory structure:

bash
molecule/
  default/
    molecule.yml
    converge.yml
    verify.yml
  centos/
    molecule.yml
    converge.yml
    verify.yml

Run specific scenario:

bash
molecule test -s centos
molecule test -s default

Fix 5: Handle Role Dependencies

Install dependencies for tests:

yaml
# molecule/default/create.yml
- name: Create
  hosts: localhost
  tasks:
    - name: Install dependencies
      command: ansible-galaxy install -r requirements.yml

Or in molecule.yml:

yaml
provisioner:
  name: ansible
  config_options:
    defaults:
      roles_path: ../../
  inventory:
    hosts:
      all:
        vars:
          required_var: value
  playbooks:
    converge: converge.yml
    verify: verify.yml

Fix 6: Fix Privilege Issues

Configure become properly:

yaml
# molecule.yml
platforms:
  - name: instance
    image: geerlingguy/docker-ubuntu2004-ansible
    privileged: true  # Required for systemd services
    command: /sbin/init
    tmpfs:
      - /run
      - /tmp
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:ro

In converge.yml:

yaml
- name: Converge
  hosts: all
  become: true
  roles:
    - myrole

Fix 7: Use Alternative Drivers

For Podman instead of Docker:

yaml
driver:
  name: podman
platforms:
  - name: instance
    image: docker.io/geerlingguy/docker-ubuntu2004-ansible
    privileged: true

For Vagrant:

yaml
driver:
  name: vagrant
provider:
  name: virtualbox
platforms:
  - name: instance
    box: ubuntu/focal64
    memory: 1024
    cpus: 1

Install driver dependencies:

bash
pip install molecule-docker
pip install molecule-podman
pip install molecule-vagrant

Fix 8: Debug Testinfra Tests

Run testinfra directly:

```bash # Inside container pytest molecule/default/tests/ -v

# From host molecule verify --debug ```

Debug specific test:

```python def test_debug(host): # Print facts print(host.system_info.type) print(host.system_info.release)

# Print file contents f = host.file('/etc/myapp.conf') print(f.content_string)

# Check multiple conditions assert True # Placeholder for debugging ```

Fix 9: Fix Sidecar Issues

For roles needing external services:

```yaml # molecule.yml platforms: - name: application image: geerlingguy/docker-ubuntu2004-ansible groups: - app links: - database:db

  • name: database
  • image: geerlingguy/docker-centos8-ansible
  • groups:
  • - db
  • `

Test connectivity between containers:

python
def test_db_connection(host):
    cmd = host.run('ping -c 1 db')
    assert cmd.rc == 0

Verifying the Fix

Run full test cycle:

bash
molecule test

Expected output: `` --> Test matrix │ └── default ├── Scenario default │ ├── Destroy │ ├── Create │ ├── Converge │ ├── Verify │ └── Destroy --> Completed test matrix

Check specific stages:

bash
molecule create
molecule list
molecule converge
molecule verify
molecule destroy

Prevention

Add Molecule to CI/CD:

yaml
# .github/workflows/molecule.yml
name: Molecule Test
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: |
          pip install molecule molecule-docker
          pip install -r requirements.txt
      - name: Run Molecule tests
        run: molecule test

Initialize role with Molecule:

bash
molecule init role myrole