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

yaml[line-length] rule can't be skipped when the triggering line is in a YAML block scalar

Open brlin-tw opened this issue 2 years ago • 2 comments

Summary

For readability and line length reasons I prefer folding long lines using YAML's block scalar functionality, however the yaml[line-length] rule can't seem to be skipped using no-qa comment when the long line is in a YAML folded/literal block scalar.

Issue Type
  • Bug Report
Ansible and Ansible Lint details
$ ansible --version
ansible [core 2.13.2]
  config file = /stripped/ansible.cfg
  configured module search path = ['/home/brlin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/brlin/.local/pipx/venvs/ansible/lib/python3.10/site-packages/ansible
  ansible collection location = /home/brlin/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/brlin/.local/bin/ansible
  python version = 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0]
  jinja version = 3.1.2
  libyaml = True

$ ansible-lint --version
WARNING: PATH altered to include /home/brlin/.local/pipx/venvs/ansible/bin
ansible-lint 6.4.0 using ansible 2.13.2

  • ansible installation method: pip(via pipx)
  • ansible-lint installation method: pip(via pipx)
OS / ENVIRONMENT

OS: Ubuntu 22.04 LTS

STEPS TO REPRODUCE
Desired Behavior

The long line should be skippable without changing the task's behavior.

Actual Behavior

The following playbook:

- name: Example PoC
  hosts: all
  become: true
  tasks:
    - name: Install cronjob
      ansible.builtin.blockinfile:
        path: /etc/crontab
        block: >- # noqa yaml[line-length]
          @reboot root
          curl
          --data key=some.very.looooooooooooooooooooooooooooooooooooooooooooooooong.value
          https://example.com

yields:

$ ansible-lint playbook.yaml 
WARNING: PATH altered to include /home/brlin/.local/pipx/venvs/ansible/bin
WARNING  Listing 1 violation(s) that are fatal
yaml: line too long (89 > 80 characters) (yaml[line-length])
playbook.yaml:11

You can skip specific rules or tags by adding them to your configuration file:
# .config/ansible-lint.yml
warn_list:  # or 'skip_list' to silence them completely
  - yaml[line-length]  # Violations reported by yamllint.

Finished with 1 failure(s), 0 warning(s) on 1 files.

The following playbook:

- name: Example PoC
  hosts: all
  become: true
  tasks:
    - name: Install cronjob
      ansible.builtin.blockinfile:
        path: /etc/crontab
        block: >-
          @reboot root
          curl
          --data key=some.very.looooooooooooooooooooooooooooooooooooooooooooooooong.value # noqa yaml[line-length]
          https://example.com

while not triggering the failure, produces unwanted results:

# BEGIN ANSIBLE MANAGED BLOCK
@reboot root curl --data key=some.very.looooooooooooooooooooooooooooooooooooooooooooooooong.value # noqa yaml[line-length] https://example.com
# END ANSIBLE MANAGED BLOCK

brlin-tw avatar Aug 09 '22 08:08 brlin-tw

You cannot rely on comments inside block scalars because they will become part of the string itself, just put that noqa line before the block start line, it should work.

ssbarnea avatar Aug 09 '22 09:08 ssbarnea

@ssbarnea

just put that noqa line before the block start line, it should work

The issue is still reproducible with the following playbook:

- name: Example PoC
  hosts: all
  become: true
  tasks:
    - name: Install cronjob
      ansible.builtin.blockinfile:
        path: /etc/crontab
        # noqa yaml[line-length]
        block: >-
          @reboot root
          curl
          --data key=some.very.looooooooooooooooooooooooooooooooooooooooooooooooooooong.value
          https://example.com
$ ansible-lint
WARNING: PATH altered to include /home/brlin/.local/pipx/venvs/ansible/bin
WARNING  Listing 1 violation(s) that are fatal
yaml: line too long (93 > 80 characters) (yaml[line-length])
playbook.yml:12

You can skip specific rules or tags by adding them to your configuration file:
# .config/ansible-lint.yml
warn_list:  # or 'skip_list' to silence them completely
  - yaml[line-length]  # Violations reported by yamllint.

Finished with 1 failure(s), 0 warning(s) on 1 files.

The yamllint configuration:

extends: relaxed

brlin-tw avatar Aug 09 '22 10:08 brlin-tw

@ssbarnea

just put that noqa line before the block start line, it should work.

I noticed that the problem is still reproducible using the current latest version(6.20.3), with the following playbook:

- name: Example PoC
  hosts: all
  become: true
  tasks:
    - name: Install cronjob
      ansible.builtin.blockinfile:
        path: /etc/crontab
        # noqa yaml[line-length]
        block: >-
          @reboot root
          curl
          --data key=some.very.looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong.value
          https://example.com

Error message:

$ ansible-lint poc.yml 
WARNING  Listing 1 violation(s) that are fatal
yaml[line-length]: Line too long (161 > 160 characters)
poc.yml:12

Read documentation for instructions on how to ignore specific rule violations.

                Rule Violation Summary                
 count tag               profile rule associated tags 
     1 yaml[line-length] basic   formatting, yaml     

Failed: 1 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'min'.

Please check it out, thanks!

brlin-tw avatar Oct 06 '23 06:10 brlin-tw

I submitted a similar issue to this for var-naming[no-role-prefix] rule. Since both are line rules, it could be the same root cause.

ShawnHardwick avatar Oct 09 '23 16:10 ShawnHardwick