AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible troubleshooting - Error 206 Jinja Spacing

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Ansible Error 206, "`jinja[spacing]`", ensures proper spacing within Jinja2 templates for improved readability and accuracy

Ansible troubleshooting - Error 206 Jinja Spacing

Introduction

Ansible, the popular open-source automation platform, provides a powerful and flexible way to automate tasks, manage configurations, and orchestrate processes. However, to ensure the maintainability and readability of Ansible playbooks, it's important to adhere to best practices and guidelines. In this article, we'll explore Ansible Error 206, "jinja[spacing]", in Ansible-Lint which is related to Jinja2 string templates. We'll discuss how this rule helps improve readability and reduce the likelihood of typos by enforcing proper spacing in Jinja2 templates within your Ansible playbooks.

See also: Ansible troubleshooting - Error 102: No Jinja2 in 'when' Conditions

The Problem: Jinja Spacing

Ansible Error 206, "jinja[spacing]", checks for the correct spacing within Jinja2 string templates. Specifically, it ensures there are spaces between variables and operators, including filters, such as {{ var_name | filter }}. Proper spacing not only enhances readability but also makes it less likely for typographical errors to occur.

Problematic Code Example:

---
- name: Example error 206
  hosts: all
  tasks:
    - name: Error 206
      ansible.builtin.debug:
      vars:
        foo: "{{some|dict2items}}" # <-- jinja[spacing]
      when: "{{ foo | bool }}" # <-- jinja[spacing] - 'when' has implicit templating

In the problematic code above, Jinja2 templates need to have the appropriate spacing between variables, operators, and filters, making the code less readable and prone to typos.

Output:

WARNING  Listing 3 violation(s) that are fatal
jinja[spacing]: Jinja2 spacing could be improved: {{ foo | bool }} -> foo | bool (warning)
206.yml:5 Task/Handler: Error 206

no-jinja-when: No Jinja2 in when. 206.yml:5 Task/Handler: Error 206

jinja[spacing]: Jinja2 spacing could be improved: {{some|dict2items}} -> {{ some | dict2items }} (warning) 206.yml:8 Jinja2 template rewrite recommendation: `{{ some | dict2items }}`.

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

Rule Violation Summary count tag profile rule associated tags 2 jinja[spacing] basic formatting (warning) 1 no-jinja-when basic deprecations

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

Correcting Jinja Spacing

Use the appropriate spacing to address Ansible Error 206 and ensure that your Jinja2 templates are correctly formatted for readability and accuracy. Here's the corrected code:
---
- name: Example error 206
  hosts: all
  tasks:
    - name: Error 206
      ansible.builtin.debug:
      vars:
        foo: "{{ some | dict2items }}"
      when: foo | bool

In the corrected code, spaces have been added between variables, operators, and filters, improving readability and reducing the likelihood of typos.

See also: Ansible troubleshooting - Error 104: Deprecated Bare Vars

Implicit Templating in Ansible

It's important to note that in Ansible, some fields like changed_when, failed_when, until, and when are considered to use implicit Jinja2 templating. This means that they do not require double curly braces {{ }}. Therefore, Ansible Error 206 may suggest the removal of the braces for these fields.

Benefits of Proper Jinja Spacing

Improved Readability: Proper spacing in Jinja2 templates makes your Ansible playbooks more readable, allowing both you and your team members to understand the code more easily. Reduced Typos: Correctly spaced Jinja2 templates reduce the chances of introducing typographical errors, ensuring the accuracy of your automation tasks. Adherence to Best Practices: Enforcing proper spacing aligns with best practices, contributing to a standardized and clean codebase. Automatic Fixing: Ansible Error 206 can be automatically fixed using the
ansible-lint --fix option, simplifying the process of ensuring proper spacing.

See also: Ansible troubleshooting - Error 105: Deprecated Module Usage

Current Limitations

It's important to be aware of the current limitations of Ansible Error 206: Jinja2 blocks with newlines may need to be reformatted as it's assumed that the user has intentionally formatted them in a specific way. Jinja2 blocks that use tilde (
~) as a binary operation are ignored because the popular Python code formatter, "black," does not support tilde as a binary operator. Jinja2 blocks that use dot notation with numbers are ignored because both Python and "black" do not allow it.

Conclusion

Ansible Error 206, "jinja[spacing]`", underscores the importance of proper spacing in Jinja2 templates within Ansible playbooks. By following this rule and ensuring correct spacing, you enhance the readability and accuracy of your code, making it more maintainable and less error-prone.

In the world of automation, attention to detail and adherence to best practices are critical for achieving reliable and efficient results. So, when working with Ansible, remember to mind your spacing in Jinja2 templates and enjoy cleaner, more dependable playbooks.

Related Articles

whitespace control in Jinja2 for Ansiblemulti-condition Ansible when clausesflush_handlers in Ansible

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home