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

ansible-lint --fix breaks the yamllint rule named line-length

Open falon opened this issue 1 year ago • 3 comments

Summary

ansible-lint --fix breaks the line-lenght rule.

Issue Type
  • Bug Report
OS / ENVIRONMENT
# ansible-lint --version
ansible-lint 24.2.0 using ansible-core:2.16.2 ansible-compat:4.1.11 ruamel-yaml:0.18.5 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE
# ansible-lint --fix .gitlab-ci.yml

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

# yamllint .gitlab-ci.yml
.gitlab-ci.yml
  34:121    error    line too long (126 > 120 characters)  (line-length)
  50:121    error    line too long (121 > 120 characters)  (line-length)

# cat /root/.config/yamllint/config
extends: default

rules:
  # 120 chars should be enough
  line-length:
    max: 120
    level: error

Desired Behavior

The fixes made by ansible-lint should pass the configured yamllint rules.

Actual Behavior

After the ansible-lint fix, yamllint fails.

If there are some restrictions like documented in inline comment indentation to one character instead of two, then these requirements should be written in docs.

falon avatar Feb 20 '24 12:02 falon

I ran into this as well even without yamllint config file. Just using default ansible-lint --fix followed by ansible-lint resulted in error on one of our lines that was multi-line it condensed down to two

shepherdjay avatar Feb 22 '24 18:02 shepherdjay

While we do use yamllint internally we do not have the same default settings, take a look at https://github.com/ansible/ansible-lint/blob/main/src/ansiblelint/yaml_utils.py#L49

@shatakshiiii Please make a documentation change to include the implicit documentation inside rule docs page.

ssbarnea avatar Feb 28 '24 14:02 ssbarnea

For me, --fix breaks: line-length, trim trailing whitespace and converts strings like:

deb:
      "https://github.com/maxmind/geoipupdate/releases/download/v{{ geoip_maxmind_geoipupdate_version }}\
      /geoipupdate_{{ geoip_maxmind_geoipupdate_version }}_linux_amd64.deb"

to:

deb: https://github.com/maxmind/geoipupdate/releases/download/v{{ geoip_maxmind_geoipupdate_version }}/geoipupdate_{{ geoip_maxmind_geoipupdate_version }}_linux_amd64.deb

with line-length error.

It also includes the text '---' in all yaml at the beginning of the file.

I have these errors since version 6.20.1 (included). My solution has been to stop using '--fix' since the checks are OK.

Tardo avatar Apr 01 '24 15:04 Tardo

Looks like while using --fix ruamel is not able to preserve the escaped newline and collapses the given multiline string. That's the reason behind yamllint failure. The workaround for this is documented over here: https://ansible.readthedocs.io/projects/lint/rules/yaml/#additional-information-for-multiline-strings

shatakshiiii avatar May 03 '24 13:05 shatakshiiii

Re-Opening this, since it's frankly unacceptable to 'fix' a valid playbook into a fatal violation with default configuration

I experience the same problem with this bare-bones playbook:

---
- name: Install some software
  hosts: all
  tasks:
    - name: Install software via command
      ansible.builtin.command:
        cmd: "/usr/bin/zypper --quiet --non-interactive install --type package --auto-agree-with-licenses --allow-unsigned-rpm \
          --no-recommends -- /tmp/some-software-latest.rpm"
      changed_when: true

ansible-lint --fix removes the linebreak. A following ansible-lint (with or without --fix) then complains about yaml[line-length]

Snowman-25 avatar Jun 13 '24 13:06 Snowman-25