Ansible-lint in a CI pipeline with many sub directories

707 views Asked by At

The current set up of my project is

/projects
/plugins
/roles/sampleRole1/...
/roles/sampleRole2/..
galaxy.yml

I want to lint each any every role that exists within "/roles" but I am having trouble figuring out a solution and running into problems.

Currently when running

ansible-lint ./roles/sampleRole/

I get

Traceback (most recent call last):
  File "/opt/homebrew/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/__main__.py", line 252, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/__main__.py", line 165, in main
    app = get_app(offline=options.offline)
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/app.py", line 252, in get_app
    _perform_mockings()
  File "/opt/homebrew/lib/python3.9/site-packages/ansiblelint/_mockings.py", line 88, in _perform_mockings
    while os.readlink(link_path) != target:
OSError: [Errno 22] Invalid argument: '/Users/mjones2/.cache/ansible-compat/121e36/collections/ansible_collections/something/something

So I created a scrip that will cd into each directory and then run ansible-lint

for dir in ./roles/*/; do (cd "$dir" && ansible-lint -v); done 

Although the output of this command does not show me the entire file location. Ex it will say

tasks/main.yml:19: yaml: line too long (173 > 160 characters) (yaml[line-length])

This would be hard to debug in a ci pipeline and I would rather know the exact source of the problem.

1

There are 1 answers

1
dschier On

Initially, I just wanted to add a comment, because there are some questions in my head. Anyway, let's try with a resolution.

I have setup a project looking like this:

├── playbooks
│   └── playbook.yml
└── roles
    ├── sample1
    │   └── tasks
    │       └── main.yml
    └── sample2
        └── tasks
            └── main.yml

When running ansible-lint playbooks/ or ansible-lint roles/sample1/ everything works as expected. I get the errors I have put in (whitespace, name too long).

Therefore, I assume that your Ansible Lint version has some issues. I am using:

$ ansible-lint --version
ansible-lint 6.8.6 using ansible 2.14.0

If this is not feasible for you, there is also a similar option to your loop and the first comment, which will produce a readable output:

find . -name "*.yml" -exec ansible-lint {} \;

I hope this helps.