When using Ansible and Molecule with Fedora 41, a few things must be adjusted so that Molecule can access the Docker image for Fedora 41.
This is probably due to some changes in the Python package and the new Dnf 5 package; if you use a default Docker Fedora 41 image, you get errors of the shape during the Molecule run:
1 |
"msg": "Could not import the libdnf5 python module using /usr/bin/python3.13 (3.13.1 ... [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]). Please install python3-libdnf5 package or ensure you have specified the correct ansible_python_interpreter. (attempted ['/usr/libexec/platform-python', '/usr/bin/python3', '/usr/bin/python'])" |
To solve the problem, you must ensure that the packages “python3” and “python3-libdnf5” are installed in the Docker container used by Molecule during testing.
You can achieve that either by providing a custom Dockerfile for Molecule:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
--- dependency: name: galaxy driver: name: docker lint: | set -e yamllint . ansible-lint platforms: - name: fedora-gnome-instance image: custom-fedora-default-ansible dockerfile: ./Dockerfile command: "" privileged: true build_image: true ... |
and this is an example of a Dockerfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
FROM fedora:41 LABEL maintainer="Lorenzo Bettini" ENV container docker ENV LC_ALL C ENV DEBIAN_FRONTEND noninteractive RUN dnf -y update && dnf clean all RUN dnf makecache \ && dnf -y install \ python3-pip \ unzip \ sudo \ which \ python3-dnf \ python3-libdnf5 \ && dnf clean all ... |
Alternatively, you can specify the pre-built Docker Fedora 41 in the Molecule file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--- dependency: name: galaxy driver: name: docker lint: | set -e yamllint . ansible-lint platforms: - name: instance-fedora image: fedora:41 pre_build_image: true provisioner: name: ansible playbooks: prepare: prepare.yml ... |
And provide a “prepare.yml” file to install the above-mentioned required packages:
1 2 3 4 5 6 7 8 |
--- - name: Prepare hosts: all gather_facts: false tasks: - name: Install python in Fedora ansible.builtin.raw: dnf install -y python3 python3-libdnf5 changed_when: false |
Note that we must use the built-in “raw” module and avoid gathering facts. Otherwise, we would get back to the original error message.
I hope you find this post helpful.