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.49k stars 663 forks source link

Key error when scanning repository #3973

Closed pb82 closed 10 months ago

pb82 commented 10 months ago
Summary

When running ansible-lint --fix against https://github.com/ansible-collections/amazon.aws.git, a KeyError is raised and ansible-lint fails:

Traceback (most recent call last):
  File "/home/peter/.local/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/__main__.py", line 402, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
             ^^^^^^^^^^^^^^
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/__main__.py", line 379, in main
    fix(runtime_options=options, result=result, rules=rules)
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/__main__.py", line 248, in fix
    _do_transform(result, options)
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/__main__.py", line 203, in _do_transform
    transformer.run()
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/transformer.py", line 118, in run
    self._do_transforms(file, ruamel_data or data, file_is_yaml, matches)
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/transformer.py", line 156, in _do_transforms
    match.rule.transform(match, file, data)
  File "/home/peter/.local/lib/python3.12/site-packages/ansiblelint/rules/name.py", line 179, in transform
    ] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
           ~~~~~~~~~~~^^^^^^^^
  File "/home/peter/.local/lib/python3.12/site-packages/ruamel/yaml/comments.py", line 851, in __getitem__
    return ordereddict.__getitem__(self, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'name'
Issue Type
OS / ENVIRONMENT
ansible-lint 6.22.1 using ansible-core:2.16.2 ansible-compat:4.1.10 ruamel-yaml:0.18.5 ruamel-yaml-clib:0.2.7
STEPS TO REPRODUCE
  1. Clone https://github.com/ansible-collections/amazon.aws.git
  2. Run ansible-lint --fix in that repository directory
Desired Behavior

ansible-lint should not fail with a KeyError and exit code 1.

Actual Behavior

ansible-lint fails with a KeyError, exit code is 1.

[peter@fedora amazon.aws]$ ansible-playbook --syntax-check playbook
ERROR! the playbook: playbook could not be found

[minimum complete verifiable example]: see above

ganeshrn commented 10 months ago

The issue is seeing while scanning the repository using Ansible Code Bot.

TamiTakamiya commented 10 months ago

The error (it may not be the only one, though) occurs with https://github.com/ansible-collections/amazon.aws/blob/main/tests/integration/targets/elb_classic_lb/tasks/schema_change.yml

$ ansible-lint --fix=all tests/integration/targets/elb_classic_lb/tasks/schema_change.yml
WARNING  Skipped installing collection dependencies due to running in offline mode.
Traceback (most recent call last):
  File "/var/tmp/work3/venv/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 402, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 379, in main
    fix(runtime_options=options, result=result, rules=rules)
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 248, in fix
    _do_transform(result, options)
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 203, in _do_transform
    transformer.run()
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/transformer.py", line 118, in run
    self._do_transforms(file, ruamel_data or data, file_is_yaml, matches)
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/transformer.py", line 156, in _do_transforms
    match.rule.transform(match, file, data)
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ansiblelint/rules/name.py", line 179, in transform
    ] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
  File "/var/tmp/work3/venv/lib64/python3.9/site-packages/ruamel/yaml/comments.py", line 851, in __getitem__
    return ordereddict.__getitem__(self, key)
KeyError: 'name'
TamiTakamiya commented 10 months ago

A KeyError occurs with https://github.com/ansible-collections/amazon.aws/blob/main/tests/integration/targets/s3_object/tasks/main.yml as well:

$ ansible-lint --fix=all tests/integration/targets/s3_object/tasks/main.yml
WARNING  Skipped installing collection dependencies due to running in offline mode.
Traceback (most recent call last):
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/bin/ansible-lint", line 8, in <module>
    sys.exit(_run_cli_entrypoint())
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 402, in _run_cli_entrypoint
    sys.exit(main(sys.argv))
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 379, in main
    fix(runtime_options=options, result=result, rules=rules)
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 248, in fix
    _do_transform(result, options)
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/__main__.py", line 203, in _do_transform
    transformer.run()
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/transformer.py", line 119, in run
    self._do_transforms(file, ruamel_data or data, file_is_yaml, matches)
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/transformer.py", line 157, in _do_transforms
    match.rule.transform(match, file, data)
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ansiblelint/rules/name.py", line 179, in transform
    ] = f"{target_task['name'][:1].upper()}{target_task['name'][1:]}"
  File "/home/ttakamiy/git/ansible-collections/amazon.aws/venv/lib64/python3.9/site-packages/ruamel/yaml/comments.py", line 851, in __getitem__
    return ordereddict.__getitem__(self, key)
KeyError: 'name'