Switching to molecule-plugins#

Ansible roles are a great way to organize and share your infrastructure-as-code between projects. Those roles can be tested with Molecule, which is a great tool to test your roles against multiple platforms and Ansible versions. Running these tests in a CI/CD pipeline is a great way to ensure that your roles are working as expected and that you don’t introduce regressions.

These tests can be executed in different forms, but the most common one is to use Docker containers. This is one of the configurations for Molecule, and it works great. The example workflow below shows how to run Molecule tests in a GitHub Actions pipeline:

Example of .github/workflows/ci.yml to run Molecule tests#
  - name: Install dependencies
    run: |
      python -m pip install --upgrade pip
      python -m pip install "${{ matrix.ansible-version }}" ansible-lint molecule molecule-docker docker flake8 flake8-bugbear flake8-docstrings flake8-import-order flake8-pylint pytest pytest-testinfra yamllint
      if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi

  - name: Lint with yamllint
    run: |
      yamllint . --format github

  - name: Lint with flake8
    run: |
      # stop the build if there are Python syntax errors or undefined names
      flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
      # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
      flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

  - name: Test with Molecule
    run: molecule test --all
    env:
      PY_COLORS: "1"
      ANSIBLE_FORCE_COLOR: "1"

However, when you run your tests in a CI/CD pipeline, you might run into the following error:

Example of a failing Molecule test with the old modules#
FileNotFoundError: [Errno 2] No such file or directory: '/opt/hostedtoolcache/Python/3.10.11/x64/lib/python3.10/site-packages/molecule_docker/driver.json'
Error: Process completed with exit code 1.

Molecule depends on plugins like to use Docker to run tests and has to be installed, but these Python modules have been archived since January 2023 and replaced by molecule-plugins. The new modules are compatible with the old ones, but have to be installed separately. The example workflow below shows how to run Molecule tests in a GitHub Actions pipeline with the new modules:

Example of .github/workflows/ci.yml to run Molecule tests#
  - name: Install dependencies
    run: |
      python -m pip install --upgrade pip
      python -m pip install "${{ matrix.ansible-version }}" ansible-lint molecule molecule-plugins[docker] docker flake8 flake8-bugbear flake8-docstrings flake8-import-order flake8-pylint pytest pytest-testinfra yamllint
      if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi

  - name: Lint with yamllint
    run: |
      yamllint . --format github

  - name: Lint with flake8
    run: |
      # stop the build if there are Python syntax errors or undefined names
      flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
      # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
      flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

  - name: Test with Molecule
    run: molecule test --all
    env:
      PY_COLORS: "1"
      ANSIBLE_FORCE_COLOR: "1"

Line 40 has the new dependency molecule-plugins[docker]. This will install the new modules and the tests will run the test successfully. But how do you fix this locally? The easiest way is to uninstall the old modules and install the new ones:

Example of uninstalling the old modules and installing the new ones#
$ pip install molecule molecule-plugins[docker]
$ pip unstall molecule-docker

Other plugins for Podman, Vagrant, and AWS are available as well. Check out the molecule-plugins repository for more information.

Warning

Ansible Molecule v5.0.0 doesn’t install all dependencies, but in v5.1.0 the change was reverted with pull request #3901.