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.
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.
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.
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.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate