Introduction
Ansible, the popular open-source automation platform, provides a straightforward and efficient way to automate tasks, manage configurations, and orchestrate processes. However, as with any tool, there are best practices to follow and potential pitfalls to avoid. In this article, we will delve into Ansible Error 104, “Deprecated Bare Vars”, which is a rule in [Ansible-Lint](/articles/ansible-lint) designed to identify potentially confusing expressions where it’s unclear whether a variable or string should be used. We will explore this error, understand its implications, and learn how to write clean, maintainable Ansible code that adheres to best practices.
The Problem: Deprecated Bare Vars
Ansible Error 104, known as “Deprecated Bare Vars”, is a valuable rule designed to maintain code clarity and consistency. This rule points out situations where it is unclear whether a given expression should be interpreted as a variable or a string. To address this, Ansible encourages users to either use the full variable syntax or convert the expression into a list of strings.
Problematic Code Example:
``yaml
---
- ansible.builtin.debug:
msg: "{{ item }}"
with_items: foo # <-- deprecated-bare-vars
`
In the code above, the variable “foo” is being referenced without any clear indication of whether it’s a variable or a string. This ambiguity can lead to confusion and potential issues down the line.
Output:
`bash
WARNING Listing 2 violation(s) that are fatal
schema[playbook]: $[0].tasks[0].with_items 'production' does not match '^\\{[\\{%](.|[\r\n])*[\\}%]\\}