ansible-lint icon indicating copy to clipboard operation
ansible-lint copied to clipboard

Unable to disable line rule on block scalar variable

Open ShawnHardwick opened this issue 1 year ago • 2 comments

Summary

When executing ansible-lint against an Ansible collection that contains a role variable that does not meet rule var-naming[no-role-prefix] and is defined using a YAML block scalar, there is no method for disabling the rule using noqa.

Issue Type
  • Bug Report
OS / ENVIRONMENT
ansible-lint 6.20.3 using ansible-core:2.15.4 ansible-compat:4.1.10 ruamel-yaml:0.17.35 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE

Within an Ansible collection:

# roles/test_role/tasks/main.yaml
---
- name: Debug task
  ansible.builtin.debug:
    msg: "{{ test_variable }}"
# roles/test_role/defaults/main.yml
---
test_variable: value  # noqa: var-naming[no-role-prefix]

# noqa: var-naming[no-role-prefix]
test_block_scalar_variable: >-
  This
  is a
  scalar block.
Desired Behavior

var-naming[no-role-prefix] rule is ignored for test_block_scalar_variable.

Actual Behavior

roles/test_role/defaults/main.yml:5: var-naming[no-role-prefix]: Variables names from within roles should use test_role_ as a prefix. (vars: test_block_scalar_variable)

Notes

Might be related to https://github.com/ansible/ansible-lint/issues/2271

ShawnHardwick avatar Oct 09 '23 16:10 ShawnHardwick

I believe that the below should work, but the YAML parser implementation in ansible-lint doesn't handle it correctly:

test_block_scalar_variable: # noqa: var-naming[no-role-prefix]
  >-
  This
  is a
  scalar block.
ERROR    Error trying to append skipped rules
Traceback (most recent call last):
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ansiblelint/skip_utils.py", line 113, in append_skipped_rules
   yaml_skip = _append_skipped_rules(pyyaml_data, lintable)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ansiblelint/skip_utils.py", line 150, in _append_skipped_rules
   ruamel_data = load_data(lintable.content)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ansiblelint/skip_utils.py", line 138, in load_data
   return yaml.load(file_text)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/main.py", line 426, in load
   return constructor.get_single_data()
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/constructor.py", line 111, in get_single_data
   node = self.composer.get_single_node()
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/composer.py", line 70, in get_single_node
   document = self.compose_document()
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/composer.py", line 92, in compose_document
   node = self.compose_node(None, None)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/composer.py", line 128, in compose_node
   node = self.compose_mapping_node(anchor)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/composer.py", line 209, in compose_mapping_node
   item_value = self.compose_node(node, item_key)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/composer.py", line 104, in compose_node
   if self.parser.check_event(AliasEvent):
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/parser.py", line 139, in check_event
   self.current_event = self.state()
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/parser.py", line 624, in parse_block_mapping_value
   self.move_token_comment(token, empty=True)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/parser.py", line 804, in move_token_comment
   token.move_old_comment(self.scanner.peek_token() if nt is None else nt, empty=empty)
 File "/home/shawn.hardwick/code/venv/ansible-latest/lib/python3.10/site-packages/ruamel/yaml/tokens.py", line 137, in move_old_comment
   raise NotImplementedError(f'overlap in comment {c!r} {tc!r}')
NotImplementedError: overlap in comment [CommentToken('# noqa: var-naming\n', line: 28, col: 55), None] [CommentToken('\n', line: 35, col: 0), None]

For now, my workaround is to use single or double quotes:

test_block_scalar_variable: # noqa: var-naming[no-role-prefix]
  "This
   is a
   scalar block."

ShawnHardwick avatar Oct 09 '23 16:10 ShawnHardwick

Note: passing an invalid YAML file with the content from the example to linter also causes an exception, and we should fail nicely instead.

ssbarnea avatar Oct 11 '23 12:10 ssbarnea

The comment must be incline, which for block scalars it means:

      var2: >- # noqa: var-naming[no-role-prefix]
        foo
        bar

ssbarnea avatar May 08 '24 18:05 ssbarnea