Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible Molecule ignores collection requirements

I like to use a git-based Ansible Collection with my role and try to test that role with Molecule. But when I run Molecule, there is no output showing that it is adding that collection nor that it's finding the module/filter during runtime.

molecule 4.0.4 using python 3.9 
ansible:2.14.1
delegated:4.0.4 from molecule
docker:2.1.0 from molecule_docker requiring collections: community.docker>=3.0.2 ansible.posix>=1.4.0

The molecule.yml contains

---
dependency:
  name: galaxy
  options:
    requirements-file: collections.yml
...

And the collections.yml contains:

---
collections:
  - name: https://github.com/acoby/ansible-collection.git
    type: git 
    version: main

During the converge tests, I'm using a filter that is available in that collection, but it only says:

$ molecule test
INFO     default scenario test matrix: dependency, lint, cleanup, destroy, syntax, create, prepare, converge, idempotence, side_effect, verify, cleanup, destroy
INFO     Performing prerun with role_name_check=0...
INFO     Set ANSIBLE_LIBRARY=/homedir/.cache/ansible-compat/0ad1ad/modules:/homedir/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/homedir/.cache/ansible-compat/0ad1ad/collections:/homedir/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/homedir/.cache/ansible-compat/0ad1ad/roles:/homedir/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
INFO     Using /homedir/.cache/ansible-compat/0ad1ad/roles/acoby.common symlink to current repository in order to enable Ansible to find the role using its expected full name.
INFO     Running default > dependency
WARNING  Skipping, missing the requirements file.
WARNING  Skipping, missing the requirements file.
INFO     Running default > lint
INFO     Lint is disabled.
INFO     Running default > cleanup
WARNING  Skipping, cleanup playbook not configured.
INFO     Running default > destroy
INFO     Sanity checks: 'docker'

PLAY [Destroy] *****************************************************************

...

INFO     Running default > syntax

playbook: /homedir/git/github/ansible-common/molecule/default/converge.yml
INFO     Running default > create

PLAY [Create] ******************************************************************

...

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=2    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0

INFO     Running default > prepare
WARNING  Skipping, prepare playbook not configured.
INFO     Running default > converge

PLAY [Converge] ****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [instance]

....
An exception occurred during task execution. 
To see the full traceback, use -vvv. The error was: #eof. 
Could not load "acoby.collection.my_urlencode": 
'Invalid plugin FQCN (acoby.collection.my_urlencode): 
unable to locate collection acoby.collection'
failed: [instance] (item={'src': 'mail.rc.j2', 'dest': '/root/.mailrc', 'mode': '0440'}) 
...

Does anybody know what is wrong? Why is the Molecule run ignoring my collections.yml and not installing it? There was an issue before Molecule 3.0.3, but that seems to be fixed.

like image 667
TRW Avatar asked Sep 12 '25 06:09

TRW


1 Answers

I didn't really fix the issue. It should worked as described, but I found two(!) different alternatives:

  1. Remove the key dependency.options.requirements-file from molecule.yml. I was surprised that then the output at the beginning changed.
INFO     Running default > dependency
WARNING  Skipping, missing the requirements file.
Cloning into '/homedir/.ansible/tmp/ansible-local-83810gxw1uelq/tmpkm0riwyv/ansible-collectionczw7j2r4'...
Already on 'main'
Your branch is up to date with 'origin/main'.
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'acoby.collection:1.2.2' to '/homedir/.cache/molecule/ansible-common/default/collections/ansible_collections/acoby/collection'
Created collection for acoby.collection:1.2.2 at /homedir/.cache/molecule/ansible-common/default/collections/ansible_collections/acoby/collection
acoby.collection:1.2.2 was installed successfully
INFO     Dependency completed successfully.
INFO     Running default > lint
...

I didn't expect that, but I was investigating, why there are twice the warning

WARNING  Skipping, missing the requirements file.

Now, one is gone. I assume the second one is the search for collections.yml. The key in molecule.yml seems to overwrite something in the Molecule dependencies. Now it works. I think this is a bug in the documentation of Molecule. I'll open a bug report.

  1. I made a workaround by adding a prepare.yml to Molecule like this:
---
- name: "Prepare Collection"
  hosts: "localhost"
  tasks:
    - name: "Run ansible-galaxy"
      ansible.builtin.command: "ansible-galaxy install --force -r collections.yml"

This prepare script runs before the converge script and is explicitly calling ansible-galaxy. After using this workaround, my Jinja templates are able to find the filters and it works on GitHub Actions too. But, I think solution one is better.

So - both ways seem to work.

like image 164
TRW Avatar answered Sep 16 '25 06:09

TRW