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:
.github/workflows/ci.yml
to run Molecule tests#37 - name: Install dependencies
38 run: |
39 python -m pip install --upgrade pip
40 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
41 if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
42 if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
43
44 - name: Lint with yamllint
45 run: |
46 yamllint . --format github
47
48 - name: Lint with flake8
49 run: |
50 # stop the build if there are Python syntax errors or undefined names
51 flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
52 # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
53 flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
54
55 - name: Test with Molecule
56 run: molecule test --all
57 env:
58 PY_COLORS: "1"
59 ANSIBLE_FORCE_COLOR: "1"
However, when you run your tests in a CI/CD pipeline, you might run into the following error:
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:
.github/workflows/ci.yml
to run Molecule tests#37 - name: Install dependencies
38 run: |
39 python -m pip install --upgrade pip
40 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
41 if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
42 if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
43
44 - name: Lint with yamllint
45 run: |
46 yamllint . --format github
47
48 - name: Lint with flake8
49 run: |
50 # stop the build if there are Python syntax errors or undefined names
51 flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
52 # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
53 flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
54
55 - name: Test with Molecule
56 run: molecule test --all
57 env:
58 PY_COLORS: "1"
59 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:
$ 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.