ansible / molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
https://ansible.readthedocs.io/projects/molecule/
MIT License
3.88k stars 662 forks source link

Molecule 4.0.4 - ANSIBLE_ROLES_PATH and ANSIBLE_COLLECTIONS_PATH no more handled #3857

Open Tcharl opened 1 year ago

Tcharl commented 1 year ago

Issue Type

`molecule converged not take environment variables into account.

previously

provisioner:
  name: ansible
  env:
    ANSIBLE_ROLES_PATH: "${MOLECULE_PROJECT_DIRECTORY}/..:${MOLECULE_PROJECT_DIRECTORY}/../community:${ANSIBLE_ROLES_PATH}"
    ANSIBLE_COLLECTIONS_PATH: "${MOLECULE_PROJECT_DIRECTORY}/../community-collections:${ANSIBLE_COLLECTIONS_PATH}"

Was working accordingly (in a mono repo environments, updates were taken 'live').

Which is no more the situation with 4.0.4.

Molecule and Ansible details

ansible --version && molecule --version

molecule 4.0.4 using python 3.10 
    ansible:2.14.4
    delegated:4.0.4 from molecule
    vagrant:2.0.0 from molecule_vagrant

Molecule installation method (one of):

Ansible installation method (one of):

Detail any linters or test runners used:

Desired Behavior

Using

 ANSIBLE_ROLES_PATH: "${MOLECULE_PROJECT_DIRECTORY}/..:...

Let roles that are located in the same parent folder to be resolved instead of the remote dependency, which is useful when implementing a big chain of dependent roles (avoid tagging the dependency, ansible-galaxy referencing, ...).

Is it possible to get the same behavior (another way), or to support the feature again?

Actual Behavior


INFO     Set ANSIBLE_LIBRARY=/Users/charliemordant/.cache/ansible-compat/44ddf8/modules:/Users/charliemordant/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/Users/charliemordant/.cache/ansible-compat/44ddf8/collections:/Users/charliemordant/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/Users/charliemordant/.cache/ansible-compat/44ddf8/roles:/Users/charliemordant/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
zhan9san commented 1 year ago

@Tcharl

Thanks for reporting this issue.

If I understand it correctly, the X-problem is to share the dependency and the Y-problem is to support ANSIBLE_ROLES_PATH environment variable.

I'll take a look at this issue.

trallnag commented 1 year ago

Is this maybe related to my problem that Ansible / Molecule is failing to find the role in the project directory?

Error log:

  ERROR! the role 'trallnag.awscli' was not found in /home/runner/work/ansible-role-awscli/ansible-role-awscli/molecule/default/roles:/home/runner/.cache/ansible-compat/52a4ae/roles:/home/runner/.cache/molecule/ansible-role-awscli/default/roles:/home/runner/work/ansible-role-awscli:/home/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/runner/work/ansible-role-awscli/ansible-role-awscli/molecule/default

  The error appears to be in '/home/runner/work/ansible-role-awscli/ansible-role-awscli/molecule/default/converge.yml': line 15, column 15, but may
  be elsewhere in the file depending on the exact syntax problem.

  The offending line appears to be:

        ansible.builtin.include_role:
          name: trallnag.awscli
                ^ here

It is the same locally. It only works when I install the role locally into ~/.cache/ansible-compat/52a4ae/roles. But shouldn't it detect the project role automatically?

I tried setting

provisioner:
  name: ansible
  ANSIBLE_ROLES_PATH: "../../"

But this did not help.

ansible~=7.4
molecule~=5.0
molecule-plugins[docker]~=23.4
docker~=6.0

Maybe related: https://github.com/ansible-community/molecule/issues/3806

Tcharl commented 1 year ago

I ended up using these variable as a prefix of the command (and ensure that it's systematic through tox) and creating two different requirements.yml files: local and standalone so that I don't import my local roles in cache.

Not optimal but works :-).

Will publish a sample repo soon (while I'll get something acceptable to be public)

trallnag commented 1 year ago

What I don't understand why it works for some people / repositories without specification of ANSIBLE_ROLES_PATH anywhere. For example here: https://github.com/geerlingguy/ansible-role-apache

I found it out. Molecule does not consider .yaml. It only looks for .yml.

zhan9san commented 1 year ago

@Tcharl

Now the collections and roles would all be installed in the default directory(~/.ansible/collections and ~/.ansible/roles), which is shipped via isolated=False

You don't need to define ANSIBLE_COLLECTIONS_PATH and ANSIBLE_ROLES_PATH explicitly.

Could you verify whether it works?

Tcharl commented 1 year ago

Hi @zhan9san , and thanks for your help, I whish I knew python and its ecosystem better ^^

It doesn't answer to the use case, see for example https://github.com/OsgiliathEnterprise/servicemesh.

I typically rely on roles only (I do not use collection at all, overkill/not really fitting with the use case). I try to make all my roles atomic as possible: example role_nginx, role_virtualhost. And I usually use a 'monorepo' approach locally:

workspace_root
|_role_nginx
|_role_virtualhost

When I code the higher-level role (the role_virtualhost here), I usually also adapt the roles in dependency to enhance it. I also want speed while doing it, so want to avoid releasing my modifications on the child role to galaxy (role_nginx) to test my parent role (role_virtualhost) with molecule, taking into account the modifications made on the child (that aren't released, neither gitted yet). I also don't want to test my child role with molecule (sometimes it's not worth to molecule everything).

I used to play with these variables in the molecule.yml file to let it work this way, and now forced to use another framework (tox) to inject these variable at execution

zhan9san commented 1 year ago

@Tcharl

Please allow me to recap your idea. You hope molecule can support the local roles as dependencies instead of installing them from remote galaxy. This does make sense.

Have you had a workaround? Do you still need it fixing?