molecule: ANSIBLE_ROLES_PATH not set correctly?

Issue Type

  • Bug report

Molecule and Ansible details

ansible --version && molecule --version

5.1.0 and 3.5.1

Molecule installation method (one of):

  • source
  • pip

Have tried both methods.

Ansible installation method (one of):

  • source
  • pip
  • OS package

pip

Desired Behavior

Molecule tests that have been passing, should continue to be passing. When running in CI, if roles are installed, the ANSIBLE_ROLES_PATH points to the roles.

Actual Behaviour

Running molecule ansible tests in CircleCI.

Certain tests that succeeded one month ago are failing now, even though nothing apparently changed. It’s not impossible, that this is being caused by something else entirely such as Ansible itself or another package. However at the moment it looks like molecule so your feedback would be helpful to point in the right direction.


Case 1: https://app.circleci.com/pipelines/github/cloudalchemy/ansible-prometheus?branch=master&filter=all

When molecule ran on Dec 27, 2021, it succeeded.
Here is a interesting snippet from the log output:

INFO     Added ANSIBLE_ROLES_PATH=/home/circleci/.cache/ansible-compat/dda4ac/roles:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
INFO     Using /home/circleci/.ansible/roles/cloudalchemy.prometheus symlink to current repository in order to enable Ansible to find the role using its expected full name.

Notice that it’s creating a symlink.

Now,

INFO     Set ANSIBLE_ROLES_PATH=/home/circleci/.cache/ansible-compat/dda4ac/roles:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles

The wording changed from “Added” to “Set”. So, something changed. And it does not mention the symlink anymore. Probably no symlink is being created. Because of that the test fails, during “molecule syntax”. It can’t find the role.


Case 2:

This repo used to succeed, about a month ago. https://app.circleci.com/pipelines/github/prometheus/demo-site/833/workflows/39439543-5173-4027-8be0-3d1817db7b7c/jobs/3397

Now it’s failing: https://app.circleci.com/pipelines/github/prometheus/demo-site/857/workflows/82accec5-9ae4-4c17-ba50-9c25d35b550e/jobs/3516

When does it fail? During the tests, it gets to this step:

run molecule --debug syntax

Again, it can’t find the roles. Similar to Case 1.

Where are the roles being installed? It can be seen that ansible-galaxy installs roles in /home/circleci/project/roles/

- extracting cloudalchemy.prometheus to /home/circleci/project/roles/cloudalchemy.prometheus

If we test with ansible 5.1 and molecule version 3.4.1, the tests succeed. Here is log output regarding ANSIBLE_ROLES_PATH

INFO     Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles
ANSIBLE_ROLES_PATH: /home/circleci/.cache/molecule/project/default/roles:/home/circleci:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles
ANSIBLE_ROLES_PATH=/home/circleci/.cache/molecule/project/default/roles:/home/circleci:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles 

Next, test with ansible 5.1 and molecule 3.5.1. This fails.

The wording has changed from “Added” to “Set”. Why? And, more importantly, the “Set” did not accomplish anything because it didn’t set the path.

The debug information (lines 3 and 4) shows something different from the “Set” (line 1).

INFO     Set ANSIBLE_ROLES_PATH=/home/circleci/.cache/ansible-compat/dda4ac/roles:roles:/home/circleci/project/roles
DEBUG: ANSIBLE ENVIRONMENT:
ANSIBLE_ROLES_PATH: '/home/circleci/.cache/molecule/project/default/roles:/home/circleci:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:'
ANSIBLE_ROLES_PATH=/home/circleci/.cache/molecule/project/default/roles:/home/circleci:/home/circleci/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles: ANSIBLE_VAULT_PASSWORD=******* MOLECULE_DEBUG=True 

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 20 (10 by maintainers)

Commits related to this issue

Most upvoted comments

The issue is caused by the changes to the ansible_compat.runtime.Runtime.prepare_environment method that molecule calls at https://github.com/ansible-community/molecule/blob/a8d96aa86199e73347013aba7752d5dd73f68ed3/src/molecule/command/base.py#L111.

In version 1.0.0, ansible-compat added a parameter to the method, https://github.com/ansible-community/ansible-compat/blob/v1.0.0/src/ansible_compat/runtime.py#L299, called install_local. The parameter is set to False by default, which skips the installation of the collection or role.

Changing the invocation from scenario.config.runtime.prepare_environment() to scenario.config.runtime.prepare_environment(install_local=True) should fix the issue.

If you want, I can test the change and make a pull request.

This issue is not fully related to merged fix.

The main reason is in that molecule doesn’t look at the env variables exposed by ansible-compat during prerun. molecule uses ansible-compat runtime in isolated mode - compat installs roles and collections to own .cache directory, but molecule doesn’t know anything about it.

Here is the example of ‘ANSIBLE_COLLECTIONS_PATH’ and ‘ANSIBLE_ROLES_PATH’ variables:

INFO     Performing prerun...
INFO     Set ANSIBLE_LIBRARY=/home/runner/.cache/ansible-compat/d035ce/modules:/home/runner/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/home/runner/.cache/ansible-compat/d035ce/collections:/home/runner/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/home/runner/.cache/ansible-compat/d035ce/roles:/home/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
INFO     Running default > dependency
Molecule default > dependency
  INFO     Running ansible-galaxy collection install -v community.general:>=4.1.0
  WARNING  Skipping, missing the requirements file.
  WARNING  Skipping, missing the requirements file.
INFO     Running default > lint
Molecule default > lint
INFO     Running default > cleanup
Molecule default > cleanup
INFO     Running default > destroy
Molecule default > destroy
  DEBUG: ANSIBLE ENVIRONMENT:
  ANSIBLE_COLLECTIONS_PATH: /home/runner/.cache/molecule/test_command_init_scenario0/default/collections:/home/runner/.ansible/collections:/usr/share/ansible/collections:/etc/ansible/collections
  ANSIBLE_ROLES_PATH: '/home/runner/.cache/molecule/test_command_init_scenario0/default/roles:/tmp/pytest-of-runner/pytest-3:/home/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:'