ansible-lint icon indicating copy to clipboard operation
ansible-lint copied to clipboard

Unexpected behavior resolving collections, if ansible-lint is executed outside project dir

Open xrow opened this issue 1 year ago • 8 comments

Summary

Issue resolving paths to local collections when ansible-lint is not executed in project dir. It loos like tit will not alter or change the path from collections_path with is ./.

Issue Type
  • Bug Report
OS / ENVIRONMENT
[root@localhost ansible]# ansible-lint --version
ansible-lint 6.22.0 using ansible-core:2.15.6 ansible-compat:4.1.10 ruamel-yaml:None ruamel-yaml-clib:None
A new release of ansible-lint is available: 6.22.0 → 6.22.1 Upgrade by running: pip install --upgrade ansible-lint
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE

have a local collection with collections_path = ~/.ansible/collections:/usr/share/ansible/collections:./

[root@localhost infrastructure]# ansible-lint ansible/
WARNING  Listing 4 violation(s) that are fatal
syntax-check[specific]: the role 'xxx.infrastructure.vault' was not found in /scm/infrastructure/ansible/ansible_collections/xxx/infrastructure/playbooks/roles:/root/.cache/ansible-compat/9fec08/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/scm/infrastructure/ansible/ansible_collections/xxx/infrastructure/playbooks
ansible/ansible_collections/xxx/infrastructure/playbooks/local.yml:18:15

syntax-check[missing-file]: Unable to retrieve file contents
ansible/playbooks/add-chart.yml:1:1 Could not find or access '/scm/infrastructure/ansible/playbooks/xxx.infrastructure.local' on the Ansible Controller.

syntax-check[missing-file]: Unable to retrieve file contents
ansible/playbooks/build.yml:1:1 Could not find or access '/scm/infrastructure/ansible/playbooks/xxx.infrastructure.local' on the Ansible Controller.

syntax-check[missing-file]: Unable to retrieve file contents
ansible/playbooks/clean-all.yml:1:1 Could not find or access '/scm/infrastructure/ansible/playbooks/xxx.infrastructure.local' on the Ansible Controller.


                    Rule Violation Summary                     
 count tag                        profile rule associated tags 
     3 syntax-check[missing-file] min     core, unskippable    
     1 syntax-check[specific]     min     core, unskippable    

Failed: 4 failure(s), 0 warning(s) on 43 files.
A new release of ansible-lint is available: 6.22.0 → 6.22.1 Upgrade by running: pip install --upgrade ansible-lint
[root@localhost infrastructure]# ansible-lint -p ansible/
WARNING  Listing 4 violation(s) that are fatal
ansible/ansible_collections/xxx/infrastructure/playbooks/local.yml:18:15: syntax-check[specific]: the role 'xxx.infrastructure.vault' was not found in /scm/infrastructure/ansible/ansible_collections/xxx/infrastructure/playbooks/roles:/root/.cache/ansible-compat/9fec08/roles:/root/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/scm/infrastructure/ansible/ansible_collections/xxx/infrastructure/playbooks
ansible/playbooks/add-chart.yml:1:1: syntax-check[missing-file]: Unable to retrieve file contents
ansible/playbooks/build.yml:1:1: syntax-check[missing-file]: Unable to retrieve file contents
ansible/playbooks/clean-all.yml:1:1: syntax-check[missing-file]: Unable to retrieve file contents

                    Rule Violation Summary                     
 count tag                        profile rule associated tags 
     3 syntax-check[missing-file] min     core, unskippable    
     1 syntax-check[specific]     min     core, unskippable    

Failed: 4 failure(s), 0 warning(s) on 43 files.
A new release of ansible-lint is available: 6.22.0 → 6.22.1 Upgrade by running: pip install --upgrade ansible-lint
[root@localhost infrastructure]# cd ansible/ && ansible-lint .
WARNING  Skipped installing collection dependencies due to running in offline mode.

Passed: 0 failure(s), 0 warning(s) on 5 files. Last profile that met the validation criteria was 'production'.
A new release of ansible-lint is available: 6.22.0 → 6.22.1 Upgrade by running: pip install --upgrade ansible-lint
Desired Behavior

ansible-lint -p ansible/ shall work the same as cd ansible/ && ansible-lint .

Possible security bugs should be reported via email to [email protected]

Actual Behavior

See the /scm/infrastructure/ansible missing in the failed run.

[root@localhost ansible]# ansible-lint -p . -vvvv
INFO     Set ANSIBLE_COLLECTIONS_PATH=/root/.cache/ansible-compat/0cb87f/collections:/root/.ansible/collections:/usr/share/ansible/collections:/scm/infrastructure/ansible
[root@localhost infrastructure]# ansible-lint -p ansible/ -vvvv
INFO     Set ANSIBLE_COLLECTIONS_PATH=/root/.cache/ansible-compat/9fec08/collections:/root/.ansible/collections:/usr/share/ansible/collections

Related: https://github.com/ansible/ansible-compat/pull/343

xrow avatar Dec 21 '23 06:12 xrow

I think that this is not a bug because when you pass -p you force ansible lint to assume a different project instead of the detected one. Different is to be expected.

ssbarnea avatar Dec 27 '23 13:12 ssbarnea

I too am experiencing a similar issue.

I execute ansible-lint from the root dir of the project, it then traverses into a number of different roles which each have molecule files in them. It then fails on each molecule/prepare.yml saying syntax-check[specific]: the role 'xyz.common.rolename' was not found in <big list of search locations> . Yet this collection has already been fetched as apart of the intilization. To get it to run I have to use --exclude molecule/*/prepare.yml otherwise it keeps failing on the syntax-check stage.

zac90 avatar Apr 05 '24 06:04 zac90

I'm also facing this. I also tried setting --config-file. E.g.:

ansible-lint --config-file=./ansible/.config/ansible-lint.yml

But ansible-lint insists on going up to the location of the .git folder rather than the parent directory of the config file (as seen from the log message Identified <repo root> as project root due .git directory.

Has anyone found a work-around? It's annoying because pre-commit doesn't easily allow changing the CWD when running ansible-lint.

garymm avatar Apr 11 '24 04:04 garymm