Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Jinja2 spacing could be improved" error when using carriage return character in key value #2671

Closed
hoalex opened this issue Nov 11, 2022 · 2 comments · Fixed by #3059
Closed
Assignees
Labels

Comments

@hoalex
Copy link

hoalex commented Nov 11, 2022

Summary
Issue Type
  • Bug Report
Ansible and Ansible Lint details
ansible --version

ansible [core 2.12.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/hoalex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/hoalex/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
  jinja version = 2.10.1
  libyaml = True

ansible-lint --version

ansible-lint 6.8.6 using ansible 2.12.10
  • ansible installation method: pip
  • ansible-lint installation method: pip
OS / ENVIRONMENT

Ubuntu 20.04.5 LTS on Windows 10 Pro WSL
Python 3.8.10

STEPS TO REPRODUCE

I am using the cisco.ios.ios_command to send CLI commands to Cisco IOS switches. Some of these commands require user input, often by simply confirming via carriage return.
The official documentation also shows this syntax (see last example): cisco.ios.ios_command module
The playbook itself is working as it should, however, ansible-lint complains about Jinja spacing when using the carriage return character (\r) as a value for the "answer" key.

The problem is not specific to this module, I just wanted to give a real-world use case. It can also be reproduced by simply printing a carriage return via ansible.builtin.debug:

---
- name: Test carriage return
  hosts: switches
  tasks:
    - name: Test carriage return
      ansible.builtin.debug:
        msg: "\r"

Desired Behavior

No Jinja spacing error should be raised.

Actual Behavior

A Jina spacing error is raised when using a carriage return character (\r) as a key value.
The error is not raised for other special characters, such as \n (line feed) or \t (tab).

  • playbook
---
- name: Upload and stage new IOS software image
  hosts: switches
  tasks:
    - name: Upload firmware file
      cisco.ios.ios_command:
        commands:
          - command: 'copy {{ path }} flash:'
            prompt: 'Destination filename \[{{ system_image_filename }}\]?'
            answer: "\r"

  • output of running ansible-lint
WARNING  Overriding detected file kind 'yaml' with 'playbook' for given positional argument: Untitled-1.ansible.yml
WARNING  Listing 1 violation(s) that are fatal
jinja[spacing]: Jinja2 spacing could be improved:  ->
 (warning)
Untitled-1.ansible.yml:5 Jinja2 template rewrite recommendation: `
`.


              Rule Violation Summary
 count tag            profile rule associated tags
     1 jinja[spacing] basic   formatting (warning)

Passed with min profile: 0 failure(s), 1 warning(s) on 1 files.
@hoalex hoalex added bug new Triage required labels Nov 11, 2022
@hoalex hoalex changed the title "Jinja2 spacing could be improved" error when using carriage return in key value "Jinja2 spacing could be improved" error when using carriage return character in key value Nov 11, 2022
@ssbarnea ssbarnea self-assigned this Feb 17, 2023
@ssbarnea
Copy link
Member

I digged a little bit on this one and it seems to be a bug or limitation on jinja2 library, more exactly the lex() implementation which converts any \r int a \n when it does the tokenizations. I looks for environment settings that I could change to alter this behavior but i found none.

@ssbarnea
Copy link
Member

ssbarnea commented Feb 17, 2023

Based on https://github.com/pallets/jinja/blob/b7cb6ee6675b12a027c5e7518f832b2926dfe293/tests/test_lexnparse.py#L115 I would say that there is no way for you to prevent conversion of a \r to a newline with jinja templates.

Apparently there are some changes made in jinja 3.0 release which kinda make this not configurable.

If anyone has an idea about what we can do to avoid this, I am quite happy to hear it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants