Ansible troubleshooting - Error avoid-implicit
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
The avoid-implicit rule in Ansible identifies and advises against implicit behaviors, promoting explicit instructions for predictable playbook execution.

Introduction
Ansible is a powerful automation tool, but its flexibility can sometimes lead to unintended and implicit behaviors in your playbooks. These implicit behaviors are often undocumented, making it challenging to understand what's happening behind the scenes. In this article, we'll explore the "avoid-implicit" rule in Ansible and how you can follow best practices to avoid these implicit behaviors.
See also: Ansible troubleshooting - Error 102: No Jinja2 in 'when' Conditions
What is the "avoid-implicit" Rule?
The "avoid-implicit" rule is a part of Ansible's linting tool that helps identify and flag the use of implicit behaviors within your playbooks. Implicit behaviors are actions that Ansible takes without explicit instructions, and they can lead to unpredictable outcomes or errors.
Common Implicit Behaviors
One common example of implicit behavior in Ansible is when using theansible.builtin.copy module to write file content. While you might expect to provide content as a simple dictionary, Ansible can interpret this in unexpected ways. To avoid this, it's best to use an explicit Jinja template.
See also: Ansible troubleshooting - Error 104: Deprecated Bare Vars
Problematic Code
Here's an example of problematic code and the correct way to address it:
- name: Example playbook
hosts: all
tasks:
- name: Write file content
ansible.builtin.copy:
content: { "foo": "bar" } # Avoid implicit behavior
dest: /tmp/foo.txt
Output
WARNING Listing 2 violation(s) that are fatal
avoid-implicit: Avoid implicit behaviors
avoid-implicit.yml:4 Task/Handler: Write file content
risky-file-permissions: File permissions unset or incorrect.
avoid-implicit.yml:4 Task/Handler: Write file content
Read documentation for instructions on how to ignore specific rule violations.
Rule Violation Summary
count tag profile rule associated tags
1 avoid-implicit safety unpredictability
1 risky-file-permissions safety unpredictability
Failed: 2 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'moderate'. Rating: 2/5 star
Correct Code
In this code, the content is provided as a dictionary, which Ansible may interpret as file content, leading to unexpected results. It's always best to use an explicit Jinja template, as shown in the corrected code:
- name: Example playbook
hosts: all
tasks:
- name: Write file content
vars:
content: { "foo": "bar" }
ansible.builtin.copy:
content: "{{ content | to_json }}" # Avoid implicit behavior
dest: /tmp/foo.txt
By using explicit Jinja templates, you ensure that Ansible understands your intentions, reducing the chances of implicit behaviors causing issues.
See also: Ansible troubleshooting - Error 105: Deprecated Module Usage
Why Avoid Implicit Behaviors
Avoiding implicit behaviors in your Ansible playbooks is essential for several reasons: Predictability: Implicit behaviors can lead to unpredictable outcomes, making it challenging to anticipate the results of your tasks. Debugging: When implicit behaviors cause issues, debugging can be time-consuming and frustrating. Using explicit instructions makes troubleshooting much more manageable. Documentation: Implicit behaviors are often undocumented, making it harder for you and your team to understand how a playbook works. Explicit code is self-documenting and improves the playbook's readability. Maintainability: As your playbooks grow and evolve, avoiding implicit behaviors ensures that your automation remains consistent and maintainable over time.Best Practices for Avoiding Implicit Behaviors
To follow best practices for avoiding implicit behaviors in Ansible, consider the following tips: Use Explicit Instructions: Always provide explicit instructions in your playbooks. Avoid shortcuts or implicit behaviors that can lead to confusion. Read Module Documentation: Familiarize yourself with Ansible module documentation to understand how each module expects input and behaves. This will help you avoid implicit behaviors. Test Thoroughly: Testing is crucial in Ansible. Before using your playbooks in production, test them in a controlled environment to catch any implicit behaviors or unexpected outcomes. Leverage Ansible Lint: Ansible provides a linting tool that can automatically flag implicit behaviors in your playbooks. Incorporate Ansible linting into your development process to catch issues early.Conclusion
Avoiding implicit behaviors in Ansible playbooks is a best practice that leads to more predictable, maintainable, and well-documented automation. By using explicit instructions and following module documentation, you can ensure that your playbooks behave as expected. Additionally, incorporating Ansible linting into your workflow will help you catch and address implicit behaviors before they become problematic in production.Related Articles
• template lookups in Ansible • Ansible JSON Conversion Guide • handler best practices in Ansible • task gating with Ansible when • using become for sudo in AnsibleCategory: troubleshooting
Watch the video: Ansible troubleshooting - Error avoid-implicit — Video Tutorial