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.51k stars 665 forks source link

ansible-lint crashes when no .gitignore is existent #3994

Closed DirectorSloan closed 7 months ago

DirectorSloan commented 10 months ago
Summary

When running ansible-lint against single roles it breaks with complaining about missing .gitignore. Due to I am checking single ansible roles, I've no need for using a .gitignore. When creating an empty .gitignore file it works.

Issue Type
OS / ENVIRONMENT

gitlab pipeline with registry.gitlab.com/pipeline-components/ansible-lint:latest image. Tried with 0.62.0 and 0.63.0.

ansible-lint --version
ansible-lint 6.22.2 using ansible-core:2.16.2 ansible-compat:4.1.11 ruamel-yaml:0.18.5 ruamel-yaml-clib:0.2.8

Runs in a gitlab pipeline with the image registry.gitlab.com/pipeline-components/ansible-lint:latest. The ci file for the ansible-lint part looks like this:

---

ansible-lint:
  image: registry.gitlab.com/pipeline-components/ansible-lint:latest
  before_script:
    # fixes ansible.cfg in world-writable dir (if repo includes ansible.cfg)
    # https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
    - chmod -R go-w .
    - ansible-lint --version
  script:
    - ansible-lint -v
        --format codeclimate
        --show-relpath
        $(git ls-files | sort -u)
      | python -m json.tool | tee ansible-lint.json
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    reports:
      codequality: ansible-lint.json
  variables:
    # forces ansible-lint to cache downloaded collections/roles inside the
    # project dir which is required for caching to work
    XDG_CACHE_HOME: .ansible-lint-cache

...
STEPS TO REPRODUCE
git stage . 
git commit -m 'debug: something new to check'
git push
Desired Behavior

Pipeline to recognize code errors or exiting with status code 0 for no errors.

Actual Behavior

Crashing with python error:

Traceback (most recent call last):
  File "/app/bin//ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
             ^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/ansiblelint/__main__.py", line 402, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
             ^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/ansiblelint/__main__.py", line 350, in main
    rules = RulesCollection(
            ^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/ansiblelint/rules/__init__.py", line 417, in __init__
    for rule in load_plugins(rulesdirs_str):
  File "/app/lib/python3.12/site-packages/ansiblelint/rules/__init__.py", line 354, in load_plugins
    import_module(f"{f.stem}")
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/app/lib/python3.12/site-packages/ansiblelint/rules/yaml_rule.py", line 25, in <module>
    class YamllintRule(AnsibleLintRule, TransformMixin):
  File "/app/lib/python3.12/site-packages/ansiblelint/rules/yaml_rule.py", line 32, in YamllintRule
    config = load_yamllint_config()
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/ansiblelint/yaml_utils.py", line 107, in load_yamllint_config
    config_override = YamlLintConfig(file=str(file))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/yamllint/config.py", line 44, in __init__
    self.parse(content)
  File "/app/lib/python3.12/site-packages/yamllint/config.py", line 113, in parse
    self.ignore = pathspec.PathSpec.from_lines('gitwildmatch', f)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/pathspec/pathspec.py", line 212, in from_lines
    patterns = [pattern_factory(line) for line in lines if line]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/fileinput.py", line 251, in __next__
    line = self._readline()
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/fileinput.py", line 370, in _readline
    self._file = open(self._filename, self._mode, encoding=encoding, errors=self._errors)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '.gitignore'
Workaround

Adding an empty .gitignore file to the repository fixes the issues.

HTH

ssbarnea commented 7 months ago

I was not able to reproduce this bug with current version of ansible-lint.