diff --git a/docs/usage.md b/docs/usage.md index 14f4a9ebe2..e27f5a62d4 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -160,25 +160,25 @@ playbook. In cases like this, Ansible-lint can incorrectly trigger rule violations. To disable rule violations for specific tasks, and mute false positives, add -`# noqa [rule_id]` to the end of the line. It is best practice to add a comment +`# noqa: [rule_id]` to the end of the line. It is best practice to add a comment that explains why rules are disabled. -You can add the `# noqa [rule_id]` comment to the end of any line in a task. You -can also skip multiple rules with a space-separated list. +You can add the `# noqa: [rule_id]` comment to the end of any line in a task. +You can also skip multiple rules with a space-separated list. ```yaml - name: This task would typically fire git-latest and partial-become rules - become_user: alice # noqa git-latest partial-become + become_user: alice # noqa: git-latest partial-become ansible.builtin.git: src=/path/to/git/repo dest=checkout ``` -If the rule is line-based, `# noqa [rule_id]` must be at the end of the line. +If the rule is line-based, `# noqa: [rule_id]` must be at the end of the line. ```yaml -- name: This would typically fire LineTooLongRule 204 and jinja[spacing] +- name: This would typically fire jinja[spacing] get_url: - url: http://example.com/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/file.conf # noqa 204 - dest: "{{dest_proj_path}}/foo.conf" # noqa jinja[spacing] + url: http://example.com/file.conf + dest: "{{dest_proj_path}}/foo.conf" # noqa: jinja[spacing] ``` If you want Ansible-lint to skip a rule entirely, use the `-x` command line diff --git a/examples/playbooks/noqa-nested.yml b/examples/playbooks/noqa-nested.yml index 8a6e366978..505741ee62 100644 --- a/examples/playbooks/noqa-nested.yml +++ b/examples/playbooks/noqa-nested.yml @@ -5,5 +5,5 @@ block: - name: 2nd level block: - - ansible.builtin.debug: # noqa unnamed-task + - ansible.builtin.debug: # noqa: unnamed-task msg: "test unnamed task in block" diff --git a/examples/playbooks/noqa.yml b/examples/playbooks/noqa.yml index 7ef9a52aeb..a69c2635ac 100644 --- a/examples/playbooks/noqa.yml +++ b/examples/playbooks/noqa.yml @@ -2,5 +2,5 @@ - hosts: localhost tasks: - name: This would typically fire latest[git] and partial-become - become_user: alice # noqa latest[git] partial-become + become_user: alice # noqa: latest[git] partial-become git: src=/path/to/git/repo dest=checkout diff --git a/examples/playbooks/run-once-fail.yml b/examples/playbooks/run-once-fail.yml index f24d52cb4e..8963196e66 100644 --- a/examples/playbooks/run-once-fail.yml +++ b/examples/playbooks/run-once-fail.yml @@ -5,7 +5,7 @@ strategy: free gather_facts: false tasks: - # - name: Task with run_once # noqa run-once[task] (Corrected code example) + # - name: Task with run_once # noqa: run-once[task] (Corrected code example) - name: Task with run_once ansible.builtin.debug: msg: "Test" diff --git a/examples/playbooks/test_skip_inside_yaml.yml b/examples/playbooks/test_skip_inside_yaml.yml index f60f197ba0..44d6c28dc2 100644 --- a/examples/playbooks/test_skip_inside_yaml.yml +++ b/examples/playbooks/test_skip_inside_yaml.yml @@ -5,14 +5,14 @@ - skip_ansible_lint # should disable error at playbook level tasks: - name: Test # <-- 0 latest[hg] - action: ansible.builtin.hg # noqa fqcn[canonical] - - name: Test latest[hg] (skipped) # noqa latest[hg] fqcn[canonical] + action: ansible.builtin.hg # noqa: fqcn[canonical] + - name: Test latest[hg] (skipped) # noqa: latest[hg] fqcn[canonical] action: ansible.builtin.hg - name: Test latest[git] and partial-become action: ansible.builtin.git become_user: alice - - name: Test latest[git] and partial-become (skipped) # noqa latest[git] partial-become + - name: Test latest[git] and partial-become (skipped) # noqa: latest[git] partial-become action: ansible.builtin.git become_user: alice @@ -24,8 +24,8 @@ - name: Test YAML and jinja[spacing] (skipped) ansible.builtin.get_url: # noqa: risky-file-permissions - url: http://example.com/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/file.conf # noqa yaml[line-length] - dest: "{{dest_proj_path}}/foo.conf" # noqa jinja[spacing] + url: http://example.com/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/really_long_path/file.conf # noqa: yaml[line-length] + dest: "{{dest_proj_path}}/foo.conf" # noqa: jinja[spacing] - name: Test deprecated-command-syntax # <-- 3 deprecated-command-syntax ansible.builtin.command: creates=B chmod 644 A # noqa: no-free-form diff --git a/examples/roles/meta_noqa/meta/main.yml b/examples/roles/meta_noqa/meta/main.yml index a8a69ae317..62ccab3b4d 100644 --- a/examples/roles/meta_noqa/meta/main.yml +++ b/examples/roles/meta_noqa/meta/main.yml @@ -1,7 +1,7 @@ --- -galaxy_info: # noqa meta-incorrect +galaxy_info: # noqa: meta-incorrect standalone: true - author: your-name # noqa meta-no-info + author: your-name # noqa: meta-no-info description: missing min_ansible_version and platforms. author default not changed license: MIT min_ansible_version: "2.10" diff --git a/src/ansiblelint/rules/key_order.py b/src/ansiblelint/rules/key_order.py index 1ddd732f94..ea0337fa31 100644 --- a/src/ansiblelint/rules/key_order.py +++ b/src/ansiblelint/rules/key_order.py @@ -90,7 +90,7 @@ def matchtask( msg: "Debug without a name" - name: Flush handlers meta: flush_handlers - - no_log: true # noqa key-order + - no_log: true # noqa: key-order shell: echo hello name: Task with no_log on top """ diff --git a/src/ansiblelint/skip_utils.py b/src/ansiblelint/skip_utils.py index 548163dd4d..3dabcde589 100644 --- a/src/ansiblelint/skip_utils.py +++ b/src/ansiblelint/skip_utils.py @@ -23,6 +23,7 @@ import collections.abc import logging +import re from functools import lru_cache from itertools import product from typing import TYPE_CHECKING, Any, Generator, Sequence @@ -47,6 +48,7 @@ _logger = logging.getLogger(__name__) _found_deprecated_tags: set[str] = set() +_noqa_comment_re = re.compile(r"^# noqa(\s|:)") # playbook: Sequence currently expects only instances of one of the two # classes below but we should consider avoiding this chimera. @@ -242,13 +244,12 @@ def traverse_yaml(obj: Any) -> None: for v in entry: if isinstance(v, CommentToken): comment_str = v.value - if comment_str.startswith("# noqa:"): + if _noqa_comment_re.match(comment_str): line = v.start_mark.line + 1 # ruamel line numbers start at 0 # column = v.start_mark.column + 1 # ruamel column numbers start at 0 lintable.line_skips[line].update( get_rule_skips_from_line(comment_str.strip()) ) - yaml_comment_obj_strings.append(str(obj.ca.items)) if isinstance(obj, dict): for _, val in obj.items(): diff --git a/test/rules/test_line_too_long.py b/test/rules/test_line_too_long.py index 63e0390fae..3c7517b232 100644 --- a/test/rules/test_line_too_long.py +++ b/test/rules/test_line_too_long.py @@ -8,7 +8,7 @@ - name: Task example debug: msg: 'This is a very long text that is used in order to verify the rule that checks for very long lines. We do hope it was long enough to go over the line limit.' -""" # noqa 501 +""" # noqa: E501 def test_long_line() -> None: diff --git a/test/test_import_include_role.py b/test/test_import_include_role.py index b72a553727..5ecfa77675 100644 --- a/test/test_import_include_role.py +++ b/test/test_import_include_role.py @@ -12,7 +12,7 @@ ROLE_TASKS_MAIN = """\ --- - name: Shell instead of command - shell: echo hello world # noqa fqcn no-free-form + shell: echo hello world # noqa: fqcn no-free-form changed_when: false """ diff --git a/test/test_skip_import_playbook.py b/test/test_skip_import_playbook.py index c844d51ec7..8e333fe343 100644 --- a/test/test_skip_import_playbook.py +++ b/test/test_skip_import_playbook.py @@ -22,7 +22,7 @@ hosts: all tasks: - - name: Should be shell # noqa command-instead-of-shell no-changed-when no-free-form + - name: Should be shell # noqa: command-instead-of-shell no-changed-when no-free-form ansible.builtin.shell: echo lol - name: Should not be imported diff --git a/test/test_skip_inside_yaml.py b/test/test_skip_inside_yaml.py index 9b6ec4015d..d90c45dc51 100644 --- a/test/test_skip_inside_yaml.py +++ b/test/test_skip_inside_yaml.py @@ -9,29 +9,29 @@ --- - ansible.builtin.debug: msg: this should fail linting due lack of name -- ansible.builtin.debug: # noqa unnamed-task +- ansible.builtin.debug: # noqa: unnamed-task msg: this should pass due to noqa comment """ ROLE_TASKS_WITH_BLOCK = """\ --- -- name: Bad git 1 # noqa latest[git] +- name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d - name: Block with rescue and always section block: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d rescue: - - name: Bad git 5 # noqa latest[git] + - name: Bad git 5 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 6 action: ansible.builtin.git a=b c=d always: - - name: Bad git 7 # noqa latest[git] + - name: Bad git 7 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 8 action: ansible.builtin.git a=b c=d @@ -55,7 +55,7 @@ def test_role_tasks_with_block(default_text_runner: RunFromText) -> None: @pytest.mark.parametrize( ("lintable", "expected"), - (pytest.param("examples/playbooks/test_skip_inside_yaml.yml", 11, id="yaml"),), + (pytest.param("examples/playbooks/test_skip_inside_yaml.yml", 10, id="yaml"),), ) def test_inline_skips( default_rules_collection: RulesCollection, lintable: str, expected: int diff --git a/test/test_skip_playbook_items.py b/test/test_skip_playbook_items.py index 5015b413a8..06729afe5a 100644 --- a/test/test_skip_playbook_items.py +++ b/test/test_skip_playbook_items.py @@ -8,12 +8,12 @@ - name: Fixture hosts: all tasks: - - name: Bad git 1 # noqa latest[git] + - name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d pre_tasks: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d @@ -24,12 +24,12 @@ - name: Fixture hosts: all tasks: - - name: Bad git 1 # noqa latest[git] + - name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d post_tasks: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d @@ -40,12 +40,12 @@ - name: Fixture hosts: all tasks: - - name: Bad git 1 # noqa latest[git] + - name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d handlers: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d @@ -56,7 +56,7 @@ - name: Fixture hosts: all tasks: - - name: Bad git 1 # noqa latest[git] + - name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d @@ -64,7 +64,7 @@ - name: Fixture 2 hosts: all tasks: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d @@ -75,23 +75,23 @@ - name: Fixture hosts: all tasks: - - name: Bad git 1 # noqa latest[git] + - name: Bad git 1 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 2 action: ansible.builtin.git a=b c=d - name: Block with rescue and always section block: - - name: Bad git 3 # noqa latest[git] + - name: Bad git 3 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 4 action: ansible.builtin.git a=b c=d rescue: - - name: Bad git 5 # noqa latest[git] + - name: Bad git 5 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 6 action: ansible.builtin.git a=b c=d always: - - name: Bad git 7 # noqa latest[git] + - name: Bad git 7 # noqa: latest[git] action: ansible.builtin.git a=b c=d - name: Bad git 8 action: ansible.builtin.git a=b c=d diff --git a/test/test_skiputils.py b/test/test_skiputils.py index e08bd470ef..cdbd8b11d1 100644 --- a/test/test_skiputils.py +++ b/test/test_skiputils.py @@ -23,12 +23,12 @@ - name: Fixture hosts: all vars: - SOME_VAR_NOQA: "Foo" # noqa var-naming + SOME_VAR_NOQA: "Foo" # noqa: var-naming SOME_VAR: "Bar" tasks: - name: "Set the SOME_OTHER_VAR" ansible.builtin.set_fact: - SOME_OTHER_VAR_NOQA: "Baz" # noqa var-naming + SOME_OTHER_VAR_NOQA: "Baz" # noqa: var-naming SOME_OTHER_VAR: "Bat" """