molecule: v6 fails due to collection path not aligning with ansible collection path

Issue Type

  • Bug report

I’m not entirely sure that this is a molecule bug, it may be a ansible-compat bug, I’m not familiar enough with the ecosystem to be certain.

However the failures started with molecule v6.0, so it should be documented here until it is fixed even if the fix is elsewhere.

Molecule and Ansible details

ansible --version && molecule --version
ansible [core 2.15.3]
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
molecule 6.0.0 using python 3.11
    ansible:2.15.3
    azure:23.4.1 from molecule_plugins
    containers:23.4.1 from molecule_plugins requiring collections: ansible.posix>=1.3.0 community.docker>=1.9.1 containers.podman>=1.8.1
    default:6.0.0 from molecule
    docker:23.4.1 from molecule_plugins requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0
    ec2:23.4.1 from molecule_plugins
    gce:23.4.1 from molecule_plugins requiring collections: google.cloud>=1.0.2 community.crypto>=1.8.0
    podman:23.4.1 from molecule_plugins requiring collections: containers.podman>=1.7.0 ansible.posix>=1.3.0
    vagrant:23.4.1 from molecule_plugins

Test environment is an official Debian bookworm-slim container, the following steps were run to create the environment

apt-get update
apt-get install --assume-yes --no-install-recommends ca-certificates python3 python3-pip python3-wheel python3-setuptools libxml2-utils locales git
python3 -m pip --no-cache-dir install --break-system-packages setuptools_rust
python3 -m pip --no-cache-dir install --break-system-packages ansible jmespath github3.py boto3 ansible-lint yamllint flake8 "molecule>5.0.0" "molecule-plugins[podman]<23.5.0"

Actual Behaviour

On running molecule test I get an exception, the tail of which is

  File "/usr/local/lib/python3.11/dist-packages/molecule/dependency/base.py", line 91, in execute
    self._config.runtime.require_collection(name, version)
  File "/usr/local/lib/python3.11/dist-packages/ansible_compat/runtime.py", line 721, in require_collection
    self.require_collection(name=name, version=version, install=False)
  File "/usr/local/lib/python3.11/dist-packages/ansible_compat/runtime.py", line 725, in require_collection
    raise InvalidPrerequisiteError(msg)
ansible_compat.errors.InvalidPrerequisiteError: Collection 'containers.podman' not found in '['/root/.ansible/collections', '/usr/share/ansible/collections']'

Details

The collection files are all installed in /usr/local/lib/python3.11/dist-packages/ansible_collections

Ansible galaxy uses the following search paths in execute_list_collection, cli/galaxy.py:1619

        collections_search_paths = (
            set(context.CLIARGS['collections_path'] or []) | default_collections_path | set(AnsibleCollectionConfig.collection_paths)
        )

The third element, AnsibleCollectionConfig.collection_paths, is set to ['/root/.ansible/collections', '/usr/local/lib/python3.11/dist-packages']

It isn’t clear to me how collection_paths gets the dist-packages element, but it is there. https://docs.ansible.com/ansible/latest/reference_appendices/config.html#collections-paths says that the default doesn’t include that path. There is no /etc/ansible/ or ~/.ansible.cfg

molecule uses ansible-compat, ansible-compat follows the documentation not the actual ansible behaviour and only searches ['/root/.ansible/collections', '/usr/share/ansible/collections']. Which is why it can’t find any of my collections.

ansible-galaxy collection list shows all the collections

ansible-galaxy collection install containers.podman community.docker ansible.posix fails with " Nothing to do. All requested collections are already installed. If you want to reinstall them, consider using --force."

ansible -c local -i 127.0.0.1, -m containers.podman.podman_image_info all finds and executes the module (which fails, but it finds and tries it)

Workaround

ansible-galaxy collection install --collections-path /usr/share/ansible/collections containers.podman community.docker ansible.posix --force

This installs the specified collections in /usr/share/ansible/collections allowing molecule to run.

(I also tried symlinking but ansible-compat doesn’t seem to follow the link)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Same issue here, can reproduce locally if I update everything to the latest releases on my Mac:

    raise InvalidPrerequisiteError(msg)
ansible_compat.errors.InvalidPrerequisiteError: Collection 'community.docker' not found in '['/Users/jgeerling/.ansible/collections', '/usr/share/ansible/collections']'

Thanks for reporting this issue.

I’ll take a look.

I don’t know if my issue is similar, but since Molecule 6.0.0, Molecule has been installing roles and collections into the roles and collections top level dirs in my roles and playbooks. It used to install it into ~/.cache/molecule/…

But then the other issue is that when Molecule actually runs the prepare stage, Ansible can’t find my role because Ansible is running from the scenario’s directory. I also find that adding ansible.cfg in the root of my project doesn’t seem to help. This only seems to work when I set ANSIBLE_ROLES_PATH: ../../roles under my scenario’s provisioner parameter.

provisioner:
  env:
    ANSIBLE_ROLES_PATH: ../../roles

But this also forces me to:

  • Do this in every scenario for every role or playbook I write
  • Do some funky stuff with .gitignore since I don’t want these external roles and collections making their way into my project

So what’s the proper solution here and am I doing this wrong?

Has there been any movement on the roles portion of this? This still seems to be an issue in the latest Molecule install. Still working as expected in molecule<6.0.0

@lod and @geerlingguy

Could you help verify whether the issue is fixed in ansible-compat 4.1.7?

@isuftin and @hulr

I believe the issue you describe is different. We are still working on it.