0

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.

  • As for the script, try the following in order to lint every YAML file, and them only: `for f in $(find ./roles -name '*.yml'); do ansible-lint -v $f; done`. – Doron Kedem-Goldman Jan 23 '23 at 23:14
  • And as for the lines being too long, you could either break the lines, see the following link: https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines, or otherwise change the default ansible-lint rules, this rule in particular could be found in the following path: `/usr/lib/python3/dist-packages/ansiblelint/rules/LineTooLongRule.py`. – Doron Kedem-Goldman Jan 23 '23 at 23:21
  • The structure your are describing looks like a [collection](https://docs.ansible.com/ansible/latest/dev_guide/developing_collections_structure.html). If this is the case and that your collection is correctly declared in a path where ansible is suppose to look for it (see `collection_path`), running `ansible-lint` should be as simple as launching it with no parameters from the collection root folder. – Zeitounator Jan 24 '23 at 09:40

1 Answers1

1

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.

dschier
  • 81
  • 6
  • Thanks! I was using 6.3 I believe. I just bumped my version up to 6.11 and it is working now, although I do have to run with sudo or I get a permission error – Michael Jones Jan 24 '23 at 15:06