2

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 isno output showing me, it is adding that collection nor it find 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'}) 
...

Has somebody an idea, what is wrong? Why is the molecule run ignoring my collectiony.yml and is not installing it. There was in issue before molecule 3.0.3, but that seems to be fixed.

TRW
  • 876
  • 7
  • 23
  • `WARNING Skipping, missing the requirements file.` <= which debugging steps did you already take to investigate on that problem? Please [edit] your question and make it a [mre]. – Zeitounator Mar 17 '23 at 10:16
  • This is actually a different requirement file. This is dependency.options.role-file and role-files is specific for "role-dependencies" in the old layout (without collection) and because the role has no dependencies to other roles (except the collection) the molecule test doesnt contains a requirements.yml, so the warning is correct, but it doesnt have something to do with collections.yml (which is the new layout with roles: and collections: in yaml), see https://molecule.readthedocs.io/en/stable/configuration.html#ansible-galaxy – TRW Mar 17 '23 at 10:27

1 Answers1

1

I didnt really fixed 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 suprised 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 didnt expect that, but I was investigating, why there are twice the warning

WARNING  Skipping, missing the requirements file.

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

2.

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 explicit 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 1 is better.

So - both ways seem to work.

TRW
  • 876
  • 7
  • 23