8000 Fix wrong environment variables being passed to ansible compat (#3404) by marble-sh · Pull Request #3516 · ansible/molecule · GitHub
[go: up one dir, main page]

Skip to content

Fix wrong environment variables being passed to ansible compat (#3404) - #3516

Merged
ssbarnea merged 2 commits into
ansible:mainfrom
marble-sh:main
Apr 21, 2022
Merged

ssbarnea merged 2 commits into
ansible:mainfrom
marble-sh:main

Conversation

@marble-sh
Copy link
Copy Markdown
Contributor

You can find a demo of this error here:
https://github.com/marblenix/molecule-bug-demo-3404/commits/main

In the body of each commit, details the commands that are needed to be run to reproduce the error with minimal setup, with the last one pushing a requirements.txt file to this branch. To see the error, it's recommended to be working on a machine that hasn't had ansible-galaxy collection install run on it before. If you are unable to reproduce the issue locally, try removing the cached roles and collections folders.

python3 -m venv .venv; source .venv/bin/activate
pip install pip wheel; pip install 'ansible-lint[core,yamllint]' 'molecule[docker,lint]'
molecule init role acme.my_new_role --driver-name docker
cd my_new_role

pip install --upgrade --force \
        git+https://github.com/ansible-community/molecule@main \
        git+https://github.com/ansible/ansible-compat@main \
        git+https://github.com/ansible/ansible@devel
molecule test

To test my changes, run:

pip install --upgrade --force git+https://github.com/marblenix/molecule@main

I ran the test suite by enabling actions on my fork. The tox parts failed when it needed the secrets, but the unit tests themselves still passed. https://github.com/marblenix/molecule/actions/runs/2193046395

Please note, this will not solve the issue with #3513. It does allow for a workaround to this bug by adding the following to a requirements.yml file:

collections:
  - community.docker

--

The exact cause of this issue is this line here in ansible-compat: https://github.com/ansible/ansible-compat/blob/05d94d4d1b484b05c53c9b5844e4187d9872c258/src/ansible_compat/runtime.py#L515

The problem is ansible-compat is adding these environment variables to the runtime, and molecule is adding its own variables to the provisioner. Combining them seems to resolve the issue just fine.

To verify that this specific problem is resolved, run tests like so: molecule --debug -v test. The very last output of DEBUG: SHELL REPLAY: should include a line about ANSIBLE_COLLECTIONS_PATH being in ~/.cache/ansible-compat/DEADBEEF/collections:...

@ssbarnea
Copy link
Copy Markdown
Member

I am wondering how were you able to run these commands considering that latest version of ansible-lint removed both "core" and "yamllint" extras, so pip install with them would fail. Are you sure you used py38 or newer?

@marble-sh
marble-sh commented Apr 20, 2022 •
Copy link
Copy Markdown
Contributor Author

Are you sure you used py38 or newer?

@ssbarnea

Thank you for asking. I tested this with my companies internal galaxy roles, and it worked there. We host them in individual private git repos, and this bug hit us pretty hard when molecule stopped working with requirements.yml.

The versions of software that were tested are as follows:

molecule 3.6.2.dev38 using python 3.9.12
    ansible:2.14.0.dev0
    delegated:3.6.2.dev38 from molecule
    docker:1.1.0 from molecule_docker requiring collections: community.docker>=1.9.1

The Jenkins server we use to run the molecule testing uses python 3.8, and this worked with ansible stable 2.12.3.

To be sure I had a clean environment, I deleted the .venv directory and started from scratch with the following commands:

python3 -m venv .venv; source .venv/bin/activate
pip install --upgrade pip wheel; pip install --upgrade 'ansible-lint[core,yamllint]' 'molecule[docker,lint]'
pip install --upgrade \
    git+https://github.com/marblenix/molecule@main \
    git+https://github.com/ansible/ansible-compat@main \
    git+https://github.com/ansible/ansible@devel

You can see the final requirements.txt I tested with here: https://github.com/marblenix/molecule-bug-demo-3404/blob/main/requirements.txt

I hope that answers your question.

@marble-sh
Copy link
Copy Markdown
Contributor Author

@ssbarnea, @webknjaz

Would either of you have a chance to look at this today?

@ssbarnea
ssbarnea merged commit 1136ea5 into ansible:main Apr 21, 2022
@ssbarnea
Copy link
Copy Markdown
Member

Lets hope I will not have to yank that fix.

@apatard
apatard commented Apr 25, 2022
Copy link
Copy Markdown
Contributor

This PR is actually regressing things (at least molecule-vagrant CI). From the logs, I can see:

ANSIBLE_LIBRARY: /Users/runner/work/molecule-vagrant/molecule-vagrant/.tox/py39/lib/python3.9/site-packages/molecule/provisioner/ansible/plugins/modules:/Users/runner/work/molecule-vagrant/molecule-vagrant/.tox/py39/lib/python3.9/site-packages/molecule_vagrant/modules:/Users/runner/.cache/molecule/XFRUV/test-scenario/library:/private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/pytest-of-runner/pytest-0/test_command_init_scenario0/XFRUV/library:/Users/runner/.ansible/plugins/modules:/usr/share/ansible/plugins/modules

vs

ANSIBLE_LIBRARY: /Users/runner/.cache/ansible-compat/1e0daf/modules:/Users/runner/.ansible/plugins/modules:/usr/share/ansible/plugins/modules

(https://github.com/ansible-community/molecule-vagrant/runs/6122171777?check_suite_focus=true)

Which means that the path to the molecule-vagrant module is lost, leading to the failures. I've yet to find time to try finding a fix.

@sio
sio commented Apr 25, 2022 •
Copy link
Copy Markdown
Contributor

This fix broke syntax check step for me. I think ANSIBLE_ROLES_PATH is now calculated incorrectly, but I'm not really sure why.

Here are the logs (roles being tested were not modified between runs):

Correct roles directory (/builds/sio/ci-with-molecule-git/ansible/roles) is missing from ANSIBLE_ROLES_PATH in the second run.

@sio
sio commented Apr 25, 2022 •
Copy link
Copy Markdown
Contributor

Uhm, if I understand this PR correctly, when a key exists both in self._config.provisioner.env and in self._config.runtime.environ only the value from runtime gets used.

I'm not sure why such order of priority has been chosen. It seems to me that it should be the other way around (or maybe an even more complex merging logic is required) since a lot of Molecule code is touching self._config.provisioner.env and these edits are obviously not safe to drop en masse.

@marblenix, can you elaborate on why your suggested order of priority should be used?

@marble-sh
Copy link
Copy Markdown
Contributor Author

Sure, the order was chosen because I assumed that the values in runtime were more important. As I was debugging and stepping through the code I did see that molecule focused on provisioner changes, but I noticed ansible-compat cared a lot about what values were set. I picked the order because I thought it'd be the safest route to keep the compat library happy.

@sio
sio commented Apr 25, 2022
Copy link
Copy Markdown
Contributor

I disagree. As I see it, the edits done by Molecule are there for a reason and it's not ok to just drop them. Broken builds reported by two people (myself included) support my intuition.

But I'm hardly an expert here. @ssbarnea, what do you think?

@marble-sh
Copy link
Copy Markdown
Contributor Author

I tested the changes I made on all of our companies projects, but if there are reported broken builds, that's definitely my fault for this assumption. I'll make a PR to switch the order today.

@apatard
Copy link
Copy Markdown
Contributor

Can't we merge variable instead of overwriting things ?

@marble-sh
Copy link
Copy Markdown
Contributor Author

@apatard , @sio , @ssbarnea

I've cleaned up this implementation and added a test. See #3529

@zhan9san
Copy link
Copy Markdown
Contributor

Hi @marblenix

To verify that this specific problem is resolved, run tests like so: molecule --debug -v test. The very last output of DEBUG: SHELL REPLAY: should include a line about ANSIBLE_COLLECTIONS_PATH being in ~/.cache/ansible-compat/DEADBEEF/collections:...

If the issue is ~/.cache/ansible-compat/DEADBEEF/collections:... is not in ANSIBLE_COLLECTIONS_PATH

Could you please consider the following PRs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

0