ansible / ansible-lint

ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
https://ansible.readthedocs.io/projects/lint/
GNU General Public License v3.0
3.44k stars 654 forks source link

Getting Err22 on MacOS X with ansible-lint 6 #2070

Closed alxgomz closed 2 years ago

alxgomz commented 2 years ago
Summary

When running ansible-lint on my project I get the error below after upgrading to ansible-lint 6.0.2. This seems to be related to the presence of the galaxy.yml file at the root of my project. It contains details about the playbook as per the ansible-galaxy format but the playbook itself has not been pushed to galaxy (it is just here waiting for maturity)

Issue Type
Ansible and Ansible Lint details
Traceback (most recent call last):
  File "/Users/me/.virtualenvs/aad/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/__main__.py", line 253, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/__main__.py", line 166, in main
    app = get_app()
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/app.py", line 210, in get_app
    _perform_mockings()
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/_mockings.py", line 87, in _perform_mockings
    if not link_path.exists() or os.readlink(link_path) != target:
OSError: [Errno 22] Invalid argument: '/Users/me/.cache/ansible-compat/a15ca0/collections/ansible_collections/alfresco/acs'
ansible --version
ansible [core 2.12.4]
  config file = /Users/Alexandre.Chapellon@hyland.com/workspace/alfresco-ansible-deployment/ansible.cfg
  configured module search path = ['/Users/Alexandre.Chapellon@hyland.com/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/Alexandre.Chapellon@hyland.com/.virtualenvs/aad/lib/python3.9/site-packages/ansible
  ansible collection location = /Users/Alexandre.Chapellon@hyland.com/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/Alexandre.Chapellon@hyland.com/.virtualenvs/aad/bin/ansible
  python version = 3.9.10 (main, Jan 15 2022, 11:48:00) [Clang 13.0.0 (clang-1300.0.29.3)]
  jinja version = 3.1.1
  libyaml = True
ansible-lint --version
ansible-lint 6.0.2 using ansible 2.12.4
OS / ENVIRONMENT

MacOSX Monterey

STEPS TO REPRODUCE
git clone https://github.com/Alfresco/alfresco-ansible-deployment.git
# switch to a python venv
pip install -r requirements.txt
ansible-lint playbook/acs.yml
Desired Behavior

Linting completes and return result

Actual Behavior

ansible-lint fails with the error bellow

Traceback (most recent call last):
  File "/Users/me/.virtualenvs/aad/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/__main__.py", line 253, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/__main__.py", line 166, in main
    app = get_app()
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/app.py", line 210, in get_app
    _perform_mockings()
  File "/Users/me/.virtualenvs/aad/lib/python3.9/site-packages/ansiblelint/_mockings.py", line 87, in _perform_mockings
    if not link_path.exists() or os.readlink(link_path) != target:
OSError: [Errno 22] Invalid argument: '/Users/me/.cache/ansible-compat/a15ca0/collections/ansible_collections/alfresco/acs'
Other info:

The same reproduction steps work ok on linux. Removing the galaxy.yml file fixes the problem.

hswong3i commented 2 years ago

@alxgomz I found a solution: sudo ansible-lint.

Background: Recently I change my Ansible Collection for Kubernetes (molecule + libvirt based, https://github.com/alvistack/ansible-collection-kubernetes) to be running with sudo during CI, so my $HOME/.cache/ansible-compat directory now belongs to root but not my current user. Because https://github.com/ansible/ansible-lint/blob/main/src/ansiblelint/_mockings.py#L70-L76 will create a symlink for mocking, in case directory permission error the symlink will not able to be created and so failed.

alxgomz commented 2 years ago

Thanks @hswong3i In my case the CI runs on linux hosts so I'm not facing the issue there. Instead it's happening on my local dev machine whe pre-commit hook kicks in. And of course I don't really to sudo git commit :)

The issue arose while I had a collections in the cache that was a folder. After I deleted it and ran lint again, it got recreated as a symlink and there's no more error. I guess that's an issue only if you migrate from env where previous versions of ansible-lint already populated the cache

hswong3i commented 2 years ago

@alxgomz yes I also find delete the existing cached folders from $HOME/.cache/ansible* help ;-)

egvimo commented 2 years ago

I have exactly the same issue since version 6. I can reproduce it locally (Ubuntu) and on CI (Molecule lint).

Also for me removing the galaxy.yml file resolves the issue.

hswong3i commented 2 years ago

Maybe a better solution with prerun: false, e.g. https://github.com/alvistack/ansible-collection-kubernetes/commit/b265db909b52a35d61cef06e6a7fac0ce1ed0416

See https://molecule.readthedocs.io/en/latest/configuration.html?highlight=galaxy#prerun for more information.

egvimo commented 2 years ago

This is not an issue with Molecule. It happens if I run ansible-lint directly. The error occurs also via Molecule because it calls Ansible lint to lint the role.

ssbarnea commented 2 years ago

AFAIK This bug does not reproduce with v6.2.1

egvimo commented 2 years ago

For me this is still happening with v6.2.1.

ssbarnea commented 2 years ago

@egvimo Can you remove the cache folder and recreate virtualenv? My main machine is a macos and i never seen this myself, i suspect a local "glitch" that you might have. We really need to find a way to reproduce it to be able to fix it.

egvimo commented 2 years ago

It is failing on CI (Ubuntu) with the linting step of Molecule:

OSError: [Errno 22] Invalid argument: '/home/runner/.cache/ansible-compat/9c3242/collections/ansible_collections/egvimo/misc'

I've tried to remove the /home/runner/.cache/ folder in the step before. So I think it is not an an issue with local caching, although I can reproduce it locally with this repository.